gpt4 book ai didi

java - Java中finalize方法的问题

转载 作者:行者123 更新时间:2023-12-02 01:49:38 24 4
gpt4 key购买 nike

在我上次的练习中,我一直在使用 Finalize 方法时遇到问题。似乎我有一个已弃用的方法,而且我似乎找不到最新版本的 Finalize。我有两个文件,需要调用“finalize”方法。有人可以给我一个完成方法的例子或者帮助我完成我的方法吗?这是我的代码:

public class RoomOccupancy {

private int numberInRoom;
private static int totalNumber;
private double roomCharges;
private boolean finalizeCalled;

public RoomOccupancy () {
this.numberInRoom = 0;
this.totalNumber = 0;
this.roomCharges = 0;
this.finalizeCalled = false;
System.out.println ( "Room Occupancy - default/no argument constructor" );
}

public RoomOccupancy (int number, double rCharges) {
numberInRoom = number;
roomCharges = rCharges;
finalizeCalled = false;
System.out.println ( "Room Occupancy - Overloaded/2-argument constructor" );
}

public static int getTotal () {
return totalNumber;
}

public void addOneToRoom () {
numberInRoom++;
totalNumber++;
}

public void removeOneFromRoom () {
numberInRoom--;
totalNumber--;
}

public int getNumber () {
return numberInRoom;
}

public void setNumber (int number) {
this.numberInRoom = number;
}

public double getCharges () {
return roomCharges;
}

public void setCharges (double rCharges) {
this.roomCharges = rCharges;
}

public String toString () {
String c;
c = " " + numberInRoom + " " + roomCharges + " ";
return c;
}

public boolean equals ( Object obj ) {
if ( this == obj ) return true;

if (( obj != null) && ( getClass() == obj.getClass())) {
RoomOccupancy d = ( RoomOccupancy ) obj;

if (( numberInRoom == d.numberInRoom) &&
( totalNumber == d.totalNumber) &&
( roomCharges == d.roomCharges)) {
return true;
}
else {
return false;
}

}
else {
return false;
}

}

public void finalize ( ) {
if ( !finalizeCalled ) {
// Do cleanup
}
System.out.println ( "Course - finalize method" );

}

public void dispose ( ) {
//Do cleanup
finalizeCalled = true;
System.out.println ( "Course - finalize method" );
}
}

import java.text.DecimalFormat;

public class RoomOccupancyTest {

public static void main ( String [] args ) {

RoomOccupancy noOccupancy = new RoomOccupancy ( );
System.out.println ("No Occupancy =" + noOccupancy);
RoomOccupancy roomA = new RoomOccupancy(0, 100.00);
RoomOccupancy roomB = new RoomOccupancy(0, 200.00);
RoomOccupancy roomC = new RoomOccupancy(0, 250.00);
DecimalFormat patternCharges = new DecimalFormat("#####0.00");

System.out.println ("Five people have checked into room A at $100.00 per person\n" +
"Four people have checked into room B at $200.00 per person.\n" +
"Three people have checked into room C at $250.00 per person.");
roomA.addOneToRoom();
roomA.addOneToRoom();
roomA.addOneToRoom();
roomA.addOneToRoom();
roomA.addOneToRoom();
roomB.addOneToRoom();
roomB.addOneToRoom();
roomB.addOneToRoom();
roomB.addOneToRoom();
roomC.addOneToRoom();
roomC.addOneToRoom();
roomC.addOneToRoom();

System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());

System.out.println ("One person from each room has left.");
roomA.removeOneFromRoom();
roomB.removeOneFromRoom();
roomC.removeOneFromRoom();

System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());

if (roomA == roomB)
System.out.println ("Room A and B are the same object."); // NOT EXPECTED
if (roomA == roomC)
System.out.println ("Room A and C are the same object."); // NOT EXPECTED
if (roomB == roomC)
System.out.println ("Room B and C are the same object."); // NOT EXPECTED
else
System.out.println ("No rooms are the same object."); // EXPECTED

if (roomA.equals (roomB))
System.out.println ( "Room A and B are EQUAL."); // NOT EXPECTED
if (roomA.equals(roomC))
System.out.println ( "Room A and C are the EQUAL."); // NOT EXPECTED
if (roomB.equals(roomC))
System.out.println ( "Room B and C are the EQUAL."); // NOT EXPECTED
else
System.out.println ( "No Rooms are EQUAL."); // EXPECTED

roomA.finalize();
roomB.finalize();
roomC.finalize();

roomA = roomB = roomC = null;
System.gc();


}
}

最佳答案

finalize 自 Java 9 起已被弃用。您可以在 javadoc 中了解原因以及替代方案。 :-

Deprecated. The finalization mechanism is inherently problematic. Finalization can lead to performance issues, deadlocks, and hangs. Errors in finalizers can lead to resource leaks; there is no way to cancel finalization if it is no longer necessary; and no ordering is specified among calls to finalize methods of different objects. Furthermore, there are no guarantees regarding the timing of finalization. The finalize method might be called on a finalizable object only after an indefinite delay, if at all. Classes whose instances hold non-heap resources should provide a method to enable explicit release of those resources, and they should also implement AutoCloseable if appropriate. The Cleaner and PhantomReference provide more flexible and efficient ways to release resources when an object becomes unreachable.

您不应该使用此方法。实际上你不需要!

如果您仍然想使用它,请忽略警告或在您的方法中添加以下内容以隐藏警告:-

@SuppressWarnings( "deprecation" )

关于java - Java中finalize方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53182849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com