黑马程序员技术交流社区

标题: 谁来给这个继承问题个合理解释(私有和继承问题) [打印本页]

作者: 常强儿    时间: 2015-1-12 22:24
标题: 谁来给这个继承问题个合理解释(私有和继承问题)
基类中有私有成员,而私有成员又被非私有的方法使用(比如getter和setter),子类如果不重写,就会拥有这些非私有方法,但是没有与之对应的成员。例如:

  1. class Bird {
  2.     private int aTest;
  3.     public int bTest;
  4.        
  5.     public Bird(int aTest, int bTest) {
  6.    <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
什么东西。。。。
  1. class Bird {
  2.         private int aTest;
  3.         public int bTest;
  4.        
  5.         public Bird(int aTest, int bTest) {
  6.                 super();
  7.                 this.aTest = bTest;
  8.                 this.bTest = bTest;
  9.         }
  10.        
  11.         @Override
  12.         public String toString () {
  13.                 return "" + aTest + "-" + bTest;
  14.         }
  15. }
  16. public class Swallow extends Bird{
  17.         public Swallow(int aTest, int bTest) {
  18.                 super(aTest, bTest);
  19.         }
  20.        
  21.         public static void main (String[] args) {
  22.                 Bird b1 = new Swallow(1, 1);
  23.                 System.out.println(b1);
  24.                
  25.                 Swallow s = new Swallow(2, 2);
  26.                 System.out.println(s);
  27.         }
  28. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2