【发布时间】:2019-09-03 15:20:01
【问题描述】:
正如我们所知,一个类可以拥有只能通过 new classname() 获取内存的对象。 那么如何为类的实例分配一些值。
class Work{
public int a = 55;
}
class DoStuff{
public static void main(String[] args){
Work obj = new Work(); // intialises variable now
System.out.println(obj.a) // a already has value 55
}
}
【问题讨论】:
-
使用getter和setter