- 修饰符 class 子类 extends 父类 {
- // ...
- }
- // Animal.java
- public class Animal{
- String name;
- int age;
- public void eat(){
- System.out.println(name + "正在吃饭");
- }
- public void sleep(){
- System.out.println(name + "正在睡觉");
- }
- }
- // Dog.java
- public class Dog extends Animal{
- void bark(){
- System.out.println(name + "汪汪汪~~~");
- }
- }
- // Cat.Java
- public class Cat extends Animal{
- void mew(){
- System.out.println(name + "喵喵喵~~~");
- }
- }
- // TestExtend.java
- public class TestExtend {
- public static void main(String[] args) {
- Dog dog = new Dog();
- // dog类中并没有定义任何成员变量,name和age属性肯定是从父类Animal中继承下来的
- System.out.println(dog.name);
- System.out.println(dog.age);
- // dog访问的eat()和sleep()方法也是从Animal中继承下来的
- dog.eat();
- dog.sleep();
- dog.bark();
- }
- }
- public class Base {
- int a;
- int b;
- }
- public class Derived extends Base{
- int c;
- public void method(){
- a = 10; // 访问从父类中继承下来的a
- b = 20; // 访问从父类中继承下来的b
- c = 30; // 访问子类自己的c
- }
- }
- public class Base {
- int a;
- int b;
- int c;
- }
- /
- public class Derived extends Base{
- int a; // 与父类中成员a同名,且类型相同
- char b; // 与父类中成员b同名,但类型不同
- public void method(){
- a = 100; // 访问子类自己新增的a
- b = 101; // 访问子类自己新增的b
- c = 102; // 子类没有c,访问的肯定是从父类继承下来的c
- }
- }
- public class Base {
- public void methodA(){
- System.out.println("Base中的methodA()");
- }
- }
- public class Derived extends Base{
- public void methodB(){
- System.out.println("Derived中的methodB()方法");
- }
- public void methodC(){
- methodB(); // 访问子类自己的methodB()
- methodA(); // 访问父类继承的methodA()
- }
- }
- public class Base {
- public void methodA(){
- System.out.println("Base中的methodA()");
- }
- public void methodB(){
- System.out.println("Base中的methodB()");
- }
- }
- public class Derived extends Base{
- public void methodA(int a) {
- System.out.println("Derived中的method(int)方法");
- }
- public void methodB(){
- System.out.println("Derived中的methodB()方法");
- }
- public void methodC(){
- methodA(); // 没有传参,访问父类中的methodA()
- methodA(20); // 传递int参数,访问子类中的methodA(int)
- methodB(); // 直接访问,则永远访问到的都是子类中的methodB(),基类的无法访问到
- }
- }
- public class Base {
- int a;
- int b;
- public void methodA(){
- System.out.println("Base中的methodA()");
- }
- public void methodB(){
- System.out.println("Base中的methodB()");
- }
- }
- public class Derived extends Base{
- int a; // 与父类中成员变量同名且类型相同
- char b; // 与父类中成员变量同名但类型不同
- // 与父类中methodA()构成重载
- public void methodA(int a) {
- System.out.println("Derived中的method()方法");
- }
- // 与基类中methodB()构成重写(即原型一致,重写后序详细介绍)
- public void methodB(){
- System.out.println("Derived中的methodB()方法");
- }
- public void methodC(){
- // 对于同名的成员变量,直接访问时,访问的都是子类的
- a = 100; // 等价于: this.a = 100;
- b = 101; // 等价于: this.b = 101;
- // 注意:this是当前对象的引用
- // 访问父类的成员变量时,需要借助super关键字
- // super是获取到子类对象中从基类继承下来的部分
- super.a = 200;
- super.b = 201;
- // 父类和子类中构成重载的方法,直接可以通过参数列表区分清访问父类还是子类方法
- methodA(); // 没有传参,访问父类中的methodA()
- methodA(20); // 传递int参数,访问子类中的methodA(int)
- // 如果在子类中要访问重写的基类方法,则需要借助super关键字
- methodB(); // 直接访问,则永远访问到的都是子类中的methodA(),基类的无法访问到
- super.methodB(); // 访问基类的methodB()
- }
- }
- public class Base {
- public Base(){
- System.out.println("Base()");
- }
- }
- public class Derived extends Base{
- public Derived(){
- // super(); // 注意子类构造方法中默认会调用基类的无参构造方法:super(),
- // 用户没有写时,编译器会自动添加,而且super()必须是子类构造方法中第一条语句,
- // 并且只能出现一次
- System.out.println("Derived()");
- }
- }
- public class Test {
- public static void main(String[] args) {
- Derived d = new Derived();
- }
- }
-
-
- 结果打印:
- Base()
- Derived()
代码块执行顺序
- class Person {
- public String name;
- public int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- System.out.println("构造方法执行");
- }
- {
- System.out.println("实例代码块执行");
- }
- static {
- System.out.println("静态代码块执行");
- }
- }
- public class TestDemo {
- public static void main(String[] args) {
- Person person1 = new Person("bit",10);
- System.out.println("============================");
- Person person2 = new Person("gaobo",20);
- }
- }
-
- //
- 静态代码块执行
- 实例代码块执行
- 构造方法执行
- ============================
- 实例代码块执行
- 构造方法执行
- class Person {
- public String name;
- public int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- System.out.println("Person:构造方法执行");
- }
- {
- System.out.println("Person:实例代码块执行");
- }
- static {
- System.out.println("Person:静态代码块执行");
- }
- }
- class Student extends Person{
- public Student(String name,int age) {
- super(name,age);
- System.out.println("Student:构造方法执行");
- }
- {
- System.out.println("Student:实例代码块执行");
- }
- static {
- System.out.println("Student:静态代码块执行");
- }
- }
- public class TestDemo4 {
- public static void main(String[] args) {
- Student student1 = new Student("张三",19);
- System.out.println("===========================");
- Student student2 = new Student("gaobo",20);
- }
-
-
- //
- Person:静态代码块执行
- Student:静态代码块执行
- Person:实例代码块执行
- Person:构造方法执行
- Student:实例代码块执行
- Student:构造方法执行
- ===========================
- Person:实例代码块执行
- Person:构造方法执行
- Student:实例代码块执行
- Student:构造方法执行
关于不同包中的继承可见性
- // 为了掩饰基类中不同访问权限在子类中的可见性,为了简单类B中就不设置成员方法了
- // extend01包中
- public class B {
- private int a;
- protected int b;
- public int c;
- int d;
- }
- // extend01包中
- // 同一个包中的子类
- public class D extends B{
- public void method(){
- // super.a = 10; // 编译报错,父类private成员在相同包子类中不可见
- super.b = 20; // 父类中protected成员在相同包子类中可以直接访问
- super.c = 30; // 父类中public成员在相同包子类中可以直接访问
- super.d = 40; // 父类中默认访问权限修饰的成员在相同包子类中可以直接访问
- }
- }
- // extend02包中
- // 不同包中的子类
- public class C extends B {
- public void method(){
- // super.a = 10; // 编译报错,父类中private成员在不同包子类中不可见
- super.b = 20; // 父类中protected修饰的成员在不同包子类中可以直接访问
- super.c = 30; // 父类中public修饰的成员在不同包子类中可以直接访问
- //super.d = 40; // 父类中默认访问权限修饰的成员在不同包子类中不能直接访问
- }
- }
- // extend02包中
- // 不同包中的类
- public class TestC {
- public static void main(String[] args) {
- C c = new C();
- c.method();
- // System.out.println(c.a); // 编译报错,父类中private成员在不同包其他类中不可见
- // System.out.println(c.b); // 父类中protected成员在不同包其他类中不能直接访问
- System.out.println(c.c); // 父类中public成员在不同包其他类中可以直接访问
- // System.out.println(c.d); // 父类中默认访问权限修饰的成员在不同包其他类中不能直接访问
- }
- }
Java中的继承

- final int a = 10;
- a = 20; // 编译出错
- final public class Animal {
- ...
- }
- public class Bird extends Animal {
- ...
- }
- // 编译出错
- Error:(3, 27) java: 无法从最终com.bit.Animal进行继承

- public class Animal {
- String name;
- int age;
- public Animal(String name, int age){
- this.name = name;
- this.age = age;
- }
- public void eat(){
- System.out.println(name + "吃饭");
- }
- }
- public class Cat extends Animal{
- public Cat(String name, int age){
- super(name, age);
- }
- @Override
- public void eat(){
- System.out.println(name+"吃鱼~~~");
- }
- }
- public class Dog extends Animal {
- public Dog(String name, int age){
- super(name, age);
- }
- @Override
- public void eat(){
- System.out.println(name+"吃骨头~~~");
- }
- }
- ///分割线///
- public class TestAnimal {
- // 编译器在编译代码时,并不知道要调用Dog 还是 Cat 中eat的方法
- // 等程序运行起来后,形参a引用的具体对象确定后,才知道调用那个方法
- // 注意:此处的形参类型必须时父类类型才可以
- public static void eat(Animal a){
- a.eat();
- }
- public static void main(String[] args) {
- Cat cat = new Cat("元宝",2);
- Dog dog = new Dog("小七", 1);
- eat(cat);
- eat(dog);
- }
- }
- //
- 运行结果:
- 元宝吃鱼~~~
- 元宝正在睡觉
- 小七吃骨头~~~
- 小七正在睡觉
