在 java 中理解 类
Understanding classes in java
所以我对
的概念有点困惑
Instance methods of a class being called without first instantiating
an object
这在 java 中如何运作?我是否必须实例化一个对象,以便我可以调用该对象的方法。
例如,下面是我的类。
public class Date{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
}
}
public class DateTest{
public static void main(String[] yolo){
Date today;
today = new Date();
today.month = "January";
today.day = 31;
today.year = 2015;
today.writeOutput();
}
}
因此我必须先实例化一些日期?我可以在不实例化某种 "date" 对象的情况下调用实例方法吗?
关于:
Hence I would have to instantiate some date first? Can I call an instance method without instantiating a "date" object of some kind?
正确。要调用 Date 的实例方法,您必须首先创建一个 Date 对象,您目前在 main 方法中正在这样做。
Date today; // declare Date variable today
today = new Date(); // create instance and assign it to today
请注意您的代码直接操作日期字段,应该避免这种情况。
语句 today = new Date();
实例化 class Date
的一个实例,并将对该实例的引用分配给变量 today
.
这允许通过 today
变量引用实例变量和方法。没有实例,这些成员将不存在。
是的,您可以通过使用静态字段和工厂方法在不实例化的情况下调用方法。
public class Date{
public static String month;
public static int day;
public static int year;
public static void writeOutput()
{
System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
}}
然后您可以进行以下操作:
Date.month = "January";
Date.day = 1;
Date.year=2015;
Date.writeOutput();
这样你就不需要实例化了。但是,这不一定是好的做法。如果不需要 class 变量,静态工厂方法很好,而是将必要的参数传递给方法。例如:
public class Date
{
public void writeOutput(String year, int day, int month)
{
System.out.println("Today is : " + month + " " + day + " , " + year);
}
}
那你可以打电话给
Date.writeOutput("January", 1, 1);
这是我评论的副本:
Can you make a dog bark if it doesn't exists? (and only the concept of
the dogs exists). Now imagine the concept of the dog as the class,
bark as a method and Rex as an instance of a dog. So yes, you need to
instanciate a class (Dog Rex = new Dog();) In order to use a method
(Rex.bark()). Of course you can use static methods that allow you to
do something like Dog.bark() but that it's not really OOP.
所以我对
的概念有点困惑Instance methods of a class being called without first instantiating an object
这在 java 中如何运作?我是否必须实例化一个对象,以便我可以调用该对象的方法。
例如,下面是我的类。
public class Date{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
}
}
public class DateTest{
public static void main(String[] yolo){
Date today;
today = new Date();
today.month = "January";
today.day = 31;
today.year = 2015;
today.writeOutput();
}
}
因此我必须先实例化一些日期?我可以在不实例化某种 "date" 对象的情况下调用实例方法吗?
关于:
Hence I would have to instantiate some date first? Can I call an instance method without instantiating a "date" object of some kind?
正确。要调用 Date 的实例方法,您必须首先创建一个 Date 对象,您目前在 main 方法中正在这样做。
Date today; // declare Date variable today
today = new Date(); // create instance and assign it to today
请注意您的代码直接操作日期字段,应该避免这种情况。
语句 today = new Date();
实例化 class Date
的一个实例,并将对该实例的引用分配给变量 today
.
这允许通过 today
变量引用实例变量和方法。没有实例,这些成员将不存在。
是的,您可以通过使用静态字段和工厂方法在不实例化的情况下调用方法。
public class Date{
public static String month;
public static int day;
public static int year;
public static void writeOutput()
{
System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
}}
然后您可以进行以下操作:
Date.month = "January";
Date.day = 1;
Date.year=2015;
Date.writeOutput();
这样你就不需要实例化了。但是,这不一定是好的做法。如果不需要 class 变量,静态工厂方法很好,而是将必要的参数传递给方法。例如:
public class Date
{
public void writeOutput(String year, int day, int month)
{
System.out.println("Today is : " + month + " " + day + " , " + year);
}
}
那你可以打电话给
Date.writeOutput("January", 1, 1);
这是我评论的副本:
Can you make a dog bark if it doesn't exists? (and only the concept of the dogs exists). Now imagine the concept of the dog as the class, bark as a method and Rex as an instance of a dog. So yes, you need to instanciate a class (Dog Rex = new Dog();) In order to use a method (Rex.bark()). Of course you can use static methods that allow you to do something like Dog.bark() but that it's not really OOP.