Java中的继承多态性和动态绑定
我有一个子类Host
,它扩展了超类User
public abstract class User
{
public String user_name;
public String toString()
{
return this.getClass() + " Name: " + this.user_name;
}
}
class Host extends User
{
public Host(String user_name)
{
this.user_name=user_name;
}
public void test()
{
System.out.println(user_name + " pass");
}
}
内主()
User _host1 = new Host("h1");
Host _host2 = new Host("h2");
System.out.println(_host1); //class Host Name: h1
System.out.println(_host2); //class Host Name: h2
_host1.test(); //this gives me an error
_host2.test(); //this is fine
我确信_host1
和_host2
都属于Host
类
我不明白的是,为什么通过动态绑定创建的_host1
无法访问类Host
内的方法test()
我错过了什么
共 (0) 个答案