Java学习笔记之继承和接口(一) super关键字、final关键字、abstract类和方法

以下是博主学java的时候记的一些笔记,分享给大家,如果有错误或者以为的话,可以在下方留言


子类与父类在同一个包中的继承性

子类会继承父类的除private之外的其他类型的成员变量和方法,继承的成员变量和方法的访问权限都不变。

子类与父类不在同一包中的继承性

子类只会继承父类中的protected/public成员变量和方法,访问权限不变,不能够继承父类的友好变量和方法。

成员变量的隐藏和方法的重写

个人理解:子类可以重新定义父类中的成员变量,简单理解成变量的隐藏,被隐藏的变量可以通过调用父类中的方法来返回父类中的被隐藏的成员变量。

例:

<span style="font-size:18px;">class People{

public double x;

public void set_x(double x){

   this.x=x;

}

public double get_double_x(){

return x;

}

}

public Student extends People{

int x;

public int get_x(){

   Return x;

}

}

public class Example_8{

  public static void main(String []args){

         Student stu=new Student();

         stu.x=98;

         System.out.println(stu.get_x());

         stu.set_x(98.98);

         double m=stu.get_double();

         System.out.println(m);

        }

}</span>

答案:

<span style="font-size:18px;"> 

     98

     98.98</span>


方法的重写规则;重写的方法必须是可以从父类中继承过来,被重写的方法必须与父类中的方法的类型、参数个数、参数的类型都完全相同才有可能被重写。允许重写方法的类型是父类方法类型的子类型。即如果父类方法的类型是“类”,重写方法的类型可以是“子类”。

例:

<span style="font-size:18px;">class People{

     public void speak(){
        System.out.println(“我是Peopel”);

     }

}

class Chinese extends People{

     public void speak(){

        System.out.println(“我是中国人”);

     }

}

class CreatPeople{

       public People creatPeople(){           //方法的类型是People类
           People p=new People();

           return p;

      }

}

class CreatChinese extends CreatPeople{      //重写的方法的类型是People类的

       public Chinese creatPeople(){        //子类Chinese,即子类型
           Chinese chinese=new Chinese();

           return chinese;

      }

}

public class Example_9{
    public static void main(String []args){

        CreatChinese creat=new CreatChinese();

        Chinese zhang=creat.creatPeople();

        zhang.speak();

}

}</span>


答案:

 

<span style="font-size:18px;">我是中国人</span>

 

注意:重写父类的方法的时候,不能降低方法的权限,可以升高权限。

 

super 关键字

使用super调用父类的构造方法

super必须是子类构造方法的头一条语句。

例:

<span style="font-size:18px;">class Student{

       int number;

       String name;

       Student(){}

       Student(int number,String name){

          this.number=number;

          this.name=name;

       }

       public int get_number(){
          return number;

       }

       public String get_name(){
         return name;

       }

 }

class UiStudent extends Student{

      boolean is;

      UiStudent(int number,String name,boolean b){
          super(number,name);                  //调用父类的构造方法,

          is=b;                                //即执行Student(number,name);

      }

      public boolean get_is(){
          return is;

      }

}

public class Example_10{
      public static void main(String []args){

          UiStudent zhang=new UiStudent(20111,”张三”,false);

          int number=zhang.get_number();

          String name=zhang.get_name();

          boolean ma=zhang.get_is();

          System.out.println(number);

          if(ma==true){
             System.out.println(“已婚”);

          }

          else{

             System.out.println(“未婚”);

          }

     }

}

 </span>


答案: 

<span style="font-size:18px;">20111 未婚</span>

使用super 操作被隐藏的成员变量和方法

个人理解:可以直接用super()、super.x、super.play()直接访问被隐藏的父类的成员变量和方法.

例:

<span style="font-size:18px;"><span style="color:#000000;">package com.neew;

class Father {

int data;

Father(int data) {

this.data = data;

}

public int putSum(int a) {

for (int i = 0; i < a; i++) {

data += i;

}

return data;

public void pu() {

System.out.println("java!");

}

}

class Son extends Father {

String name;

Son(int data, String name) {

super(data);

this.name = name;

}

public void pu() {

System.out.println("javac!!!");

}

public int putSum(int data) {

super.pu();

return data;

}

}

public class neew {

public static void main(String[] args) {

Son son = new Son(10, "java!");

System.out.println("name=" + son.putSum(20));

}

}
</span>
 

 </span>


 答案:

<span style="font-size:18px;">  java! 20</span>


 

final 关键字

final类不能被继承,即不能有子类。如果用final修饰父类中的方法,那么这个方法不允许子类重写。final常量指定初值,而且不能被修改。

例:

<span style="font-size:18px;"><span style="color:#000000;">class A{

final double PI=3.1415926;

public double get_Area(final double r){

    return PI*r*r;

}

public final void speak(){
      System.out.println(“你好,How are you?”);

}

}

class B extends A{

/*  非法,不能重写speak方法

public void speak(){
        System.out.println(“你好”);

}

}

public class Example_11{
     public static void main(String []args){

       B b=new B();

       System.out.println(b.get_Area(100));

       b.speak();

     }

}</span></span>


 

对象的上转型对象(指的是父类对象)

假设A类是B类的父类,当用子类去创建一个对象,并把这个对象的引用放到父类的对象中,即:A a=new B();

上转型对象不能操作子类新增的成员变量,不能调用子类新增的方法。

上转型对象可以访问子类继承或隐藏的成员变量,也可以调用子类继承的方法或子类重写的方法。

例:

<span style="font-size:18px;">class Anth{

      double m=12.58;

      void crySpeak(String s){

          System.out.println(s);

      }

}

class People extends Anth{
      char m=’A’;

      int n=60;

      void computer(int a,int b){

         int c=a+b;

         System.out.println(c);

 }

   void crySpeak(String s){

        System.out.println(m+”**”+s+”**”+m);

   }

}

public class Example_12{
    public static void main(String []args){

       People people =new people();

       Anth monkey=people;

       monkey.crySpeak(“I love this game”);

       monkey.n=100;            //非法,n是子类新增的成员变量

       System.our.println(monkey.m);   //操作隐藏的m

       System.out.println(people.m);   //操作子类的m

       People zhang=(people)monkey;    //强制将上转型对象转换成子类的对象

       zhang.computer(55,33);

       zhang.m=’T’;

       System.out.println(zhang.m);

}

}</span>


答案:

 

<span style="font-size:18px;">A**I love this game**A

12.58

A

88

T</span>


abstract 类和方法

对于abstract 方法,只允许声明,不允许实现,而且不允许final和abstract同时修饰一个方法。

(1)在abstract类中可以有abstract 方法,也可以有其他方法。

(2)abstract类不能用new运算符来创建对象,如果一个类是非abstract类,但继承了一个abstract类,那么这个非抽象类必须要重写这个抽象类的方法,并给出结构体。

(3)如果一个abstract类是另一个abstract 类的子类,子类可以重写父类的abstract方法,也可以继承这个方法。

例:

<span style="font-size:18px;">package com.Car;

 

abstract class Jcar{

abstract void Qdong();

abstract void Jsu();

abstract void Sche();

}

class Scar extends Jcar{

//void put_z(){

//System.out.println("hakkl!");

//}

void Qdong(){

System.out.println("踏下离合器,换到一档");

System.out.println("然后慢慢抬起离合器");

}

void Jsu(){

System.out.println("踩油门");

}

void Sche(){

System.out.println("踏下离合器,踏下刹车板");

System.out.println("然后将档位换到一档");

System.out.println();

}

}

class Zcar extends Jcar{

void Qdong(){

System.out.println("使用前进挡");

System.out.println("然后轻踩油门");

}

void Jsu(){

System.out.println("踩油门");

}

void Sche(){

System.out.println("踏下刹车板");

}

}

public class Car {

public static void main(String []args){

Jcar car=new Scar();

System.out.println("手动挡轿车的操作:");

car.Qdong();

car.Jsu();

car.Sche();

//car.put_z();

car=new Zcar();

System.out.println("自动挡轿车的操作:");

car.Qdong();

car.Jsu();

car.Sche();

}

}

 </span>

答案:

<span style="font-size:18px;">手动挡轿车的操作:

踏下离合器,换到一档

然后慢慢抬起离合器

踩油门

踏下离合器,踏下刹车板

然后将档位换到一档

 

自动挡轿车的操作:

使用前进挡

然后轻踩油门

踩油门

踏下刹车板</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值