
✅作者简介:大家好,我是橘橙黄又青,一个想要与大家共同进步的男人😉😉
🍎个人主页:再无B~U~G-CSDN博客
- // Dog.java
- public class Dog{
- string name;
- int age;
- float weight;
- public void eat(){
- System.out.println(name + "正在吃饭");
- }
- public void sleep(){
- System.out.println(name + "正在睡觉");
- }
- void Bark(){
- System.out.println(name + "汪汪汪~~~");
- }
- }
- // Cat.Java
- public class Cat{
- string name;
- int age;
- float weight;
- public void eat(){
- System.out.println(name + "正在吃饭");
- }
- public void sleep()
- {
- System.out.println(name + "正在睡觉");
- }
- void mew(){
- System.out.println(name + "喵喵喵~~~");
- }
- }
修饰符 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();
- }
- }
注意:
1. 子类会将父类中的成员变量或者成员方法继承到子类中了2. 子类继承父类之后,必须要新添加自己特有的成员,体现出与基类的不同,否则就没有必要继承了
- 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,还是子类自己新增的a?
- b = 101; // 访问父类继承的b,还是子类自己新增的b?
- c = 102; // 子类没有c,访问的肯定是从父类继承下来的c
- // d = 103; // 编译失败,因为父类和子类都没有定义成员变量b
- }
- }
- 如果访问的成员变量子类中有,优先访问自己的成员变量。
- 如果访问的成员变量子类中无,则访问父类继承下来的,如果父类也没有定义,则编译报错。
- 如果访问的成员变量与父类中成员变量同名,则优先访问自己的。
成员变量访问遵循就近原则,自己有优先自己的,如果没有则向父类中找。
1. 成员方法名字不同
- 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()
- // methodD(); // 编译失败,在整个继承体系中没有发现方法methodD()
- }
- }
- 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()
- }
- }
1. 只能在非静态方法中使用2. 在子类方法中,访问父类的成员变量和方法 。
super的其他用法在后文中介绍。
- 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()
1. 若父类显式定义无参或者默认的构造方法,在子类构造方法第一行默认有隐含的super()调用,即调用基类构 造方法2. 如果父类构造方法是带有参数的,此时需要用户为子类显式定义构造方法,并在子类构造方法中选择合适的父类构造方法调用,否则编译失败。3. 在子类构造方法中,super(...)调用父类构造时,必须是子类构造函数中第一条语句。4. super(...)只能在子类构造方法中出现一次,并且不能和this同时出现
1. 都是Java中的关键字2. 只能在类的非静态方法中使用,用来访问非静态成员方法和字段3. 在构造方法中调用时,必须是构造方法中的第一条语句,并且不能同时存在
【不同点】
1. 静态代码块先执行,并且只执行一次,在类加载阶段执行2. 当有对象创建时,才会执行实例代码块,实例代码块执行完成后,最后构造方法执行
【继承关系上的执行顺序】
案例:
- 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);
- }
- public static void main1(String[] args) {
- Person person1 = new Person("bit",10);
- System.out.println("============================");
- Person person2 = new Person("gaobo",20);
- }
- }
输出结果:


总结:
1、父类静态代码块优先于子类静态代码块执行,且是最早执行2、父类实例代码块和父类构造方法紧接着执行3、子类的实例代码块和子类构造方法紧接着再执行4、第二次实例化子类对象时,父类和子类的静态代码块都将不会再执行
- // 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); // 父类中默认访问权限修饰的成员在不同包其他类中不能直接访问
- }
- }
- 我们希望类要尽量做到 "封装", 即隐藏内部实现细节, 只暴露出 必要 的信息给类的调用者。
- 因此我们在使用的时候应该尽可能的使用 比较严格 的访问权限. 例如如果一个方法能用 private, 就尽量不要用 public.
- 另外, 还有一种 简单粗暴 的做法: 将所有的字段设为 private, 将所有的方法设为 public. 不过这种方式属于是 对访问权限的滥用, 还是更希望同学们能写代码的时候认真思考。
在现实生活中,事物之间的关系是非常复杂,灵活多样,比如:


- 时刻牢记, 我们写的类是现实事物的抽象. 而我们真正在公司中所遇到的项目往往业务比较复杂, 可能会涉及到一系列复杂的概念, 都需要我们使用代码来表示, 所以我们真实项目中所写的类也会有很多. 类之间的关系也会更加复杂.
- 但是即使如此, 我们并不希望类之间的继承层次太复杂. 一般我们不希望出现超过三层的继承关系. 如果继承层
- 次太多, 就需要考虑对代码进行重构了.
如果想从语法上进行限制继承 , 就可以使用 final 关键字
final int a = 10; a = 20; // 编译出错 String arr = " "; arr = 20; // 编译出错
2. 修饰类:表示此类不能被继承
final public class Animal {...}public class Bird extends Animal {...}// 编译出错Error :( 3 , 27 ) java : 无法从最终 com . bit . Animal 进行继
和继承类似, 组合也是一种表达类之间关系的方式, 也是能够达到代码重用的效果。组合并没有涉及到特殊的语法 (诸如 extends 这样的关键字), 仅仅是将一个类的实例作为另外一个类的字段。
- // 轮胎类
- class Tire{
- // ...
- }
- // 发动机类
- class Engine{
- // ...
- }
- // 车载系统类
- class VehicleSystem{
- // ...
- }
- class Car{
- private Tire tire; // 可以复用轮胎中的属性和方法
- private Engine engine; // 可以复用发动机中的属性和方法
- private VehicleSystem vs; // 可以复用车载系统中的属性和方法
- // ...
- }
- // 奔驰是汽车
- class Benz extend Car{
- // 将汽车中包含的:轮胎、发送机、车载系统全部继承下来
- }