我有父类和子类,其中父类具有方法getData
。我可以直接从子类调用此方法,无论是否创建对象。 我想知道如何在没有创建对象的情况下在子类中使用该方法。 (它也不是静态方法) 这是我的理解,我们必须创建一个对象来访问方法。任何人都可以解释为什么?Java-在继承中,可以在不创建类的情况下调用方法
public class testbase {
public void getData(String Data) throws IOException{
Properties prop1;
prop1= new Properties();
FileInputStream f= new FileInputStream("C:\\file.properties");
prop1.load(f);
String data= prop1.getProperty(Data);
System.out.println(data);
}
}
class testproperties_file extends testbase {
@Test
public void test_class() throws IOException{
getData("name");
}
}
你是什么意思*“没有创建对象”*?你创建了'testproperties_file'的实例吗? – UnholySheep
子实例**是**父实例。这就是完整的继承点。如果一只动物能跑,比我可以让一只狗跑,因为一只狗是一只动物。 –
当执行在子类中时,调用在'this'上。它指的是当前的对象。当你调用'test_class'方法时,它将在'testproperties_file'的对象上被调用。 – Sid