1.继承
定义:
继承是面向对象三大特征之一。可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法
格式:
public class 子类 extends 父类{}
子类:也叫派生类
父类:基类/超类
继承中子类的特点:
子类可以有父类的内容
子类还可以有自己特有的内容
2.继承的利弊
3.继承中变量的访问特点
4.super
5.继承中构造方法的访问特点
6.继承中成员方法的访问特点
7.super的内存图
输出结果
8.方法重写
9.方法重写的注意事项
10. Java中继承的注意事项
11.老师和学生案例
package Base_se.Base_011;public class People {private String name;private int age;public People() {}public People(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package Base_se.Base_011;public class Teacher extends People {public Teacher() {}public Teacher(String name, int age) {
// this.name = name;
// this.age = age;super(name, age); // 继承父类的带参构造方法
// super();// 继承父类的无参构造方法}public void teach() {System.out.println("教学");}
}
package Base_se.Base_011;public class Demo {public static void main(String[] args) {Teacher t1 = new Teacher();t1.setName("Tom");t1.setAge(12);System.out.println(t1.getName()+","+t1.getAge());t1.teach();Teacher t2 = new Teacher("A",11);System.out.println(t2.getName()+","+t2.getAge());t2.teach();}
}
12.案例猫和狗
package Base_se.Base_011;public class Animal {private String name;private int age;public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package Base_se.Base_011;public class Cat extends Animal{public Cat() {}public Cat(String name, int age) {super(name, age);}public void show(){System.out.println("抓老鼠");}}
package Base_se.Base_011;public class AnimalDEmo {public static void main(String[] args) {Cat c1 = new Cat();c1.setName("Tom");c1.setAge(12);System.out.println(c1.getName()+","+c1.getAge());c1.show();Cat c2 = new Cat("Tina",13);System.out.println(c2.getName()+","+c2.getAge());c2.show();}
}
13. packeage
14. 导包
package Base_se.Base_011.teacher;public class Teacher {public void show(){System.out.println("我是老师");}
}
package Base_se.Base_011.demo;import Base_se.Base_011.teacher.Teacher;public class Demo {public static void main(String[] args) {Teacher tc = new Teacher();tc.show();}
}
15.权限修饰符
16 状态修饰符
17 final 修饰局部变量
package Base_se.Base_011.Final;public class Student {public int age =20;
}
package Base_se.Base_011.Final;public class FinalDemo {public static void main(String[] args) {// 修饰基本类型变量final int age = 20;
// age =30;System.out.println(age);// 修饰引用类型变量final Student student = new Student();// 被final修饰了 地址不能变 内容(属性和方法)可以变student.age =100;System.out.println(student.age);// student = new Student(); // 重新给他来个地址 这是很就不行了}
}
18 修饰符 static
package Base_se.Base_011.Stu;public class Student {public String name;public int age;public static String univ;public void show() {System.out.println(name + "," + age + "," + univ);}}
package Base_se.Base_011.Stu;public class StudentDemo {public static void main(String[] args) {// 直接通过类名来访问Student.univ = "TINA";Student student = new Student();student.name = "Tom";student.age = 11;
// student.univ = "TING";student.show();Student student2 = new Student();student.name = "Toi";student.age = 12;
// student.univ = "TING";student.show();}
}
19 static 访问特点
package Base_se.Base_011.demo;public class Student {// 非静态成员变量private String name = "Tina";// 静态成员变量private static String univ = "itcast";// 非静态成员方法public void show1() {System.out.println("show");}// 非静态成员方法 可以访问静态和非静态的成员变量和方法public void show2() {System.out.println(name);System.out.println(univ);show1();show3();}// 静态成员方法public static void show3() {}// 静态成员方法 只能访问静态的成员变量和成员方法public static void show4() {
// System.out.println(name);System.out.println(univ);
// show1();show3();}
}