JAVA 子类的子类 ,父类的对象怎么调用
来源:百度知道 编辑:UC知道 时间:2024/06/30 04:24:44
如题 : Account 为父类, SavingAccount 为子类, LoanSavingAccount 为 SavingAccount子类,Account的对象怎样才能调用 LoanSavingAccount 的方法?
class Account
{
public void out()
{
System.out.println("我是Account类中的方法");
}
}
class SavingAccout extends Account
{
public void out()
{
System.out.println("我是SavingAccout中的方法");
}
}
class LoanSavingAccount extends SavingAccout
{
public void out()
{
System.out.println("我是LoanSavingAccount中的方法");
}
}
public class TestDemo
{
public static void main(String[] args)
{
Account acc=new LoanSavingAccount();
acc.out();
}
}
希望对你有所帮助
good luck to you!
在Account 里面new一个LoanSavingAccount对象,像这样:
LoanSavingAccount l = new LoanSavingAccount ();
然后你就可以通过l调用LoanSavingAccount 的方法了
干嘛要用父类对象来调子类方法?有这个必要吗?,有的话说下,我学习下
不是父类对象不能调用子类对象的方法吗?
期待中
四楼正解!鉴定完毕.
没必要啊,,,