day5 java object-oriented

Java inheritance

 

 

 

 

 

public class Mouse extends Animal {

    public Mouse(String myName, int myid) {

        super(myName, myid);

    }

}

 

Super call one of the constructor

 

 

 

 

implements keyword

Use implements keyword disguise make java with multiple inheritance characteristics, use of the class inherits the case interfaces can inherit multiple interfaces simultaneously (comma separated with the interface between the interface).

public class C implements A,B {

}

 

 

Dog d = new Dog();

this.eat (); // this call your own way

super.eat (); // super call the parent class method

 

Super () Constructor

 

Java rewrite (Override) and heavy duty (Overload)

 

 

 

When you need to call the parent class method is overridden in a subclass, to use the super keyword.

 

 

 

Java Interface

 

[Visibility] interface interface name [extends another interface name] {

        // declare variables

        // abstract method

}

 

 

/ * File Name: NameOfInterface.java * /

import java.lang.*;

// imports package

public interface NameOfInterface

{

   // any type of final, static field

   // abstract method

}

 

 

 

/ * File Name: MammalInt.java * /

public class MammalInt implements Animal{

 

   public void eat(){

      System.out.println("Mammal eats");

   }

 

   public void travel(){

      System.out.println("Mammal travels");

   }

 

   public int noOfLegs(){

      return 0;

   }

 

   public static void main(String args[]){

      MammalInt m = new MammalInt();

      m.eat();

      m.travel();

   }

}

 

Java 包(package)

 

 

/* 文件名: Animal.java */

package animals;

 

interface Animal {

   public void eat();

   public void travel();

}

Guess you like

Origin www.cnblogs.com/alyx/p/12227368.html