黑马程序员技术交流社区
标题:
谁来给这个继承问题个合理解释(私有和继承问题)
[打印本页]
作者:
常强儿
时间:
2015-1-12 22:24
标题:
谁来给这个继承问题个合理解释(私有和继承问题)
基类中有私有成员,而私有成员又被非私有的方法使用(比如getter和setter),子类如果不重写,就会拥有这些非私有方法,但是没有与之对应的成员。
例如:
class Bird {
private int aTest;
public int bTest;
public Bird(int aTest, int bTest) {
<span style="font-size: 14.3999996185303px; line-height: 30.7999992370605px;"> </span><span style="font-size: 14.3999996185303px; line-height: 30.7999992370605px;"> </span><span style="font-size: 14px; line-height: 2.2em;"> super();</span>
复制代码
谁帮忙给个合理的解释
作者:
常强儿
时间:
2015-1-12 22:26
什么东西。。。。
class Bird {
private int aTest;
public int bTest;
public Bird(int aTest, int bTest) {
super();
this.aTest = bTest;
this.bTest = bTest;
}
@Override
public String toString () {
return "" + aTest + "-" + bTest;
}
}
public class Swallow extends Bird{
public Swallow(int aTest, int bTest) {
super(aTest, bTest);
}
public static void main (String[] args) {
Bird b1 = new Swallow(1, 1);
System.out.println(b1);
Swallow s = new Swallow(2, 2);
System.out.println(s);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2