• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

青携纸笔携香

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Java十二课---继承(super与方法的重写)

继承
  • 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模

  • extends的意思是“扩展”。子类是父类的扩展

  • 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等

  • 继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示

  • 子类和父类之间,从意义上来讲应该具有“is a”的关系

  • object类

  • super

  • 方法从写

 

//父类
package com.oop.demo05;
​
//在Java中,所有的类,都默认直接或者间接继承object
//Person 人 :父类(基类)
public class Person {
​
   //public
   //protected
   //default(默认的)
   //private
   private int money=10_0000_0000;
​
   public void say(){
      System.out.println("说了一句话");
  }
​
   public int getMoney() {
       return money;
  }
​
   public void setMoney(int money) {
       this.money = money;
  }
}
​
​
//子类=====================================
​
package com.oop.demo05;
​
//学生 is 人:子类(派生类)
//子类继承父类,就会拥有父亲的全部方法!
public class Student extends Person {
//ctrl + H 打开层次结构
​
}
​
//通过子类调用父类=====================================
​
package com.oop;
​
​
import com.oop.demo05.Student;
​
public class Application {
​
   public static void main(String[] args) {
       Student student = new Student();
       student.say();
       student.setMoney(999);
       System.out.println(student.getMoney());
  }
}
​

 

 

Super注意点:

  1. super调用父类的构造方法,必须在构造方法的第一个

  2. super必须只能出现在子类的方法或者构造方法中!

  3. super和this不能同时调用构造方法

this

代表的对象不同:

  • this:本身调用者这个对象

  • super:代表父类对象的应用

前提

  • this:没有继承也可以使用

  • super:只能在继承条件下才可以使用

构造方法

  • this():本类的构造

  • super():父类的构造!

 

package com.oop.demo05;
​
//在Java中,所有的类,都默认直接或者间接继承object
//Person 人 :父类(基类)
public class Person {
​
   public Person() {
       System.out.println("Person无参执行了");
  }
​
   protected String name="kuangshen";
​
   //私有的东西无法被继承!
   public void print(){
       System.out.println("Person");
  }
​
}
//========================================
​
package com.oop.demo05;
​
//学生 is 人:子类(派生类)
//子类继承父类,就会拥有父亲的全部方法!
public class Student extends Person {
//ctrl + H 打开层次结构
​
   public Student(){
       //隐藏代码:调用了父类的无参构造
       super();//调用父类的构造器,必须要在子类构造器的第一行
       // this("hello");
       System.out.println("Student无参执行了");
  }
​
   public Student(String name) {
       this.name = name;
  }
​
   private String name="qinjiang";
​
public void print(){
    System.out.println("Student");
}
​
public void test1(){
     print();//Student
     this.print();//Student
     super.print();//Person
  }
​
public void test(String name){
    System.out.println(name);//秦疆
    System.out.println(this.name);//qinjiang
    System.out.println(super.name);//kuangshen
}
}
//========================================
package com.oop;
​
import com.oop.demo05.Person;
import com.oop.demo05.Student;
​
public class Application {
   public static void main(String[] args) {
       Student student = new Student();
      // student.test("秦疆");
       //student.test1( );
​
​
​
  }
​
}
​

 

方法的重写

 

即b是A new出来的对象,因此调用了A的方法

因为静态方法是类的方法,而非静态是对象的方法

有static时,b调用了B类的方法,因为b是用b类定义的

没有static时,b调用的是对象的方法,而b是用A类new的

package com.oop.demo05;
​
//重写都是方法的重写,和属性无关
​
public class B {
   public static  void test(){
       System.out.println("B=>test()");
  }
​
  /*
   public void test(){
       System.out.println("B=>test()");
   }
   */
}
=============================================
package com.oop.demo05;
​
//继承
public class A extends B {
   public static  void test(){
       System.out.println("A=>test()");
  }
   /*
   public void test(){
       System.out.println("A=>test()");
   }
    */
}
=========================================
package com.oop;
​
import com.oop.demo05.A;
import com.oop.demo05.B;
import com.oop.demo05.Person;
import com.oop.demo05.Student;
​
public class Application {
   public static void main(String[] args) {
      //静态的方法和非静态的方法区别很大
       //
      //方法的调用只和左边,定义的数据类型有关
       A a = new A();
       a.test();//A=>test()
​
      //父类的引用指向了子类
       B b = new A();
       b.test();//B=>test()
​
      /*非静态时
       B b = new A(); //子类重写了父类的方法
       b.test();//A=>test()
       */
​
​
  }
​
}
   
   

 

重写:需要有继承关系,子类重写父类的方法!

  1. 方法名必须相同

  2. 参数列表列表必须相同

  3. 修饰符:范围可以扩大: public>protected>default>private

  4. 抛出的异常:范围,可以被缩小,但不能扩大;Class Not Found Exception-->Exception(大)

重写,子类的方法和父类必须要一致;方法体不同!

 

为什么需要重写?

  1. 父类的功能子类不一定需要或者不一定满足!

              Alt + Insert: override;//方法的重写

posted on 2022-03-08 21:45  青携纸笔携香  阅读(322)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3