- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* 类的继承 子类可重新定义父类中已有的变量 父类中同名的变量无效(隐藏) 通过“super.变量名” 和父类名.变量名(static变量)引用 class A { int i=256, j =64; static int k = 32; final float e = 2.718f; } class B extends A { public char j=‘x’; final double k =5; static int e =321; void show() { System.out.println(i + “ “ + j + “ “ + k + “ “ + e); } void showA() { System.out.println(super.j + “ “ + A.k + “ “ + super.e); } } B b = new B(); b.show(); b.showA(); 256 x 5.0 321 64 32 2.718 this.变量名 this.方法名 this() super.变量名 super.方法名 super() super ? 当前对象/当前对象的父对象/其他 * 继承中的super对象 类的继承 父类 子类 实例化一个子类对象 this super 调用父类的 变量和方法 调用子类的 变量和方法 * 继承中的构造方法 类的继承 public class Cartoon extends Drawing { Cartoon() { System.out.println(Cartoon Constructor); } public static void main(String args[]) { Cartoon c = new Cartoon(); } } class Art { Art() { System.out.println(Art Constructor); } } class Drawing extends Art { Drawing() { System.out.println(Drawing Constructor); } } Art Constructor Drawing Constructor Cartoon Constructor 子类的构造方法 必须调用 父类的构造方法 class Drawing extends Art { /*Drawing() { System.out.println(Drawing Constructor); }*/ } Art Constructor Cartoon Constructor public class Cartoon extends Drawing { Cartoon() { super(); System.out.println(Cartoon Constructor); } public static void main(String args[]) { Cartoon c = new Cartoon(); } } * 再谈继承中的构造方法 类的继承 public class Chess extends BoardGame { Chess() { super(3); System.out.println(Cartoon Constructor); } public static void main(String args[]) { Chess c = new Chess(); } } class Game { Game(int i) { System.out.println(“Game Constructor); } } class BoardGame extends Game { BoardGame(int i) { super(i); System.out.println(“BoardGame Constructor); } } 子类的构造方法 必须要对父类的 构造方法进行 调用, 不管以任何形式, 否则编译出错 * 再谈继承中的构造方法 类的继承 class A { A (int i) {} } class B extends A { B (String s) { } } B (String s) { super(); } * 类的继承 子类可重新定义父类中已有的静态变量(static) class Point { static int x = 2; } cl
文档评论(0)