Java语言程序设计-类与对象.pptVIP

  • 3
  • 0
  • 约1.28万字
  • 约 75页
  • 2019-04-30 发布于江苏
  • 举报
* public class Cartoon extends Drawing { Cartoon() { System.out.println(Cartoon Constructor); } public static void main(String args[]) { Cartoon c = new Cartoon(); } } 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(); } } 2、继承中的构造方法 class Art { Art() { System.out.println(Art Constructor); } } * 再谈继承中的构造方法 类的继承 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); } } 子类的构造方法 必须要对父类的 构造方法进行 调用, 不管以任何形式, 否则编译出错 * 类的继承 再谈子类对父类变量的继承 import points.Point3d; class Point4d extends Point3d { int w; public void move(int dx, int dy, int dz, int dw) { x += dx; y += dy; z += dz; w += dw; } } package points; public class Point { int x, y; public void move(int dx, int dy) { x += dx; y += dy; } } package points; public class Point3d extends Point { int z; public void move(int dx, int dy, int dz) { x += dx; y += dy; z += dz; } } 目录结构 /points/Point.class /points/Point3d.class /Point4d.class import points.Point3d; class Point4d extends Point3d { int w; public void move(int dx, int dy, int dz, int dw) { super.move(dx, dy, dz); w += dw; } } 编译时报错: x, y, z仅在本包 中才能访问 继承与类成员的访问修饰符有关! * 2、类的继承 类成员访问修饰符与继承的关系 私有的(private)类成员不能被子类继承 公共的(public)和保护性的(protected)类成员能被子类继承,且子类和父类可以属于不同的包 无修饰的父类成员,仅在本包中才能被子类继承 构造函数不是类成员,所以不被继承 * 名称

文档评论(0)

1亿VIP精品文档

相关文档