java - abstract class and interface class

A .protected rights modifier

public  class ProtectedTest {
     public  static  void main (String [] args) { 
        
        // attributes and methods of the present modified protected package can be called.
        // subclass different packages may be modified to access protected properties and methods of 
        the Person Person = new new the Person (); 
    } 
}
Import com.atguigu.exer.Person; 

public  class ProtectedTest {
     public  static  void main (String [] args) { 
        the Person Person = new new the Person (); 
        Student STU = new new Student (); 
        stu.demo (); 
    } 
} 

// subclasses of different packages may be modified to access protected properties and methods 
class Student the extends the Person {
     public  void Demo () { 
        System.out.println (protectedName); 
        say (); 
    } 
}
public class Person {
    protected String protectedName;
    
    protected void say() {
        System.out.println("say");
    }
}

Two .abstract abstract class

1. Keyword: abstract (four object-oriented properties: encapsulation, inheritance, polymorphism, abstract class)

The method may be modified 2.abstract: abstract method
  abstract method no method body

3.abstract categories may be modified: abstract class
  1. Class abstract methods must be located as an abstract class
  2. abstract class can not be instantiated
  Consideration: abstract class has a constructor that ? There - as subclass instantiation process: constructor must call the parent class
  3. abstract class can not abstract methods. The method can have non-abstract
  4. The non-abstract subclasses must override the abstract method of the abstract parent class. If the parent class non-abstract method overrides the abstract method, then the sub-class would not override the abstract methods in

use with 4.abstract and not those keywords?
  private, final, static

public  class AbstractTest {
     public  static  void main (String [] args) { 
        Book Book = new new Book (); 
        book.read (); 
    } 
} 

// create an abstract class 
abstract  class Library {
     // se no practical meaning, However, considering that it must write the multi-state, so this method can be declared as abstract methods 
     public  abstract  void read (); 
} 

class Book the extends Library {
     public  void read () { 
        System.out.println ( "reading a good book, taste a full life " ); 
    } 
}

 

One of the three members of the class: code block

format:{}

Description:
  1. block of code can only be modified static

Classification code block: VS static code block non-static block

static block of code:
  1. The information is used to initialize the class
  2. With static block of code is loaded and the loaded class is loaded only once
  3. static code block can have multiple, sequential execution performed from top to bottom
  4. static block of code can only call the static class variables and methods (because of the different timing of the loading)
  loading block 5. static code block in preference to non-static

non-static code block:
  1 is used to initialize the object
  2. with the non-static block is created and the object loaded
  3. load takes precedence over non-static code constructor
  4. the non-static block can have a plurality of, in order from the execution sequentially performed on the next
   5 block non-static class variables and can call the static method

, if the code number is loaded only once, may be considered among the code blocks into a static

    {
         //     non-static block 
        System.out.println ( "{}" + the Name); 
        
    } 
    
    static {
         // static code block 
        System.out.println ( "{}. 1" + Age); 
    }

Four .final keyword

Please briefly describe the final keyword?
 final modified class: can not be inherited

final modified method: You can not be rewritten

final modified variables: can only be assigned once and must be initialized (often modified variable as a constant)
final assignment in three ways: display assignment, code block assignment, assignment constructor

* Variable assignment: assigning a default value 2. Display 3. assignment block constructor 4. The object name assignment method name /. Object Name attribute.
  * 
 * The evaluation order: 1 <- 2/3 (who in , who should assignment) <--4 <--5

Example 1

// design template 

public  class TemplateMethod {
     public  static  void main (String [] args) { 
        Code code = new new Code2 (); 
        code.runTime (); 
    } 
} 

abstract  class Code { 
    
    public  void Runtime () {
         // Calculation start code time 
        Long Start = System.currentTimeMillis (); 
        
        // execute the code to be executed 
        RunCode (); 
        
        // calculate the end time codes 
        Long end = System.currentTimeMillis (); 
        
        // calculate the time difference
        System.out.println("time=" + (end - start));
    }
    
    public abstract void runCode();
}

class Code2 extends Code{
    
    public void runCode() {
        for(int i = 1;i < 100;i++){
            if(i % 2 == 0) {
                System.out.println(i);
            }
        }
    }
}

V. interface class interface

New features in the interface when jdk1.8:

  1. In addition to the interface can have constant and abstract method, you can also have a default method and static method

  2. How to call static methods: Static method name Interface name
  how to call the default method: the implementation class the default method of the object name.

  3. principle of priority: the same parent class and a class interfaces. This method is called by the parent object of the class class method

  4. The class implements a plurality of interfaces, the default method if the same plurality of classes, the class must override this method

  5. In the class the default method of method call interface: the interface name .super default method name.

 

Interface: interface

format: interface interface name} {

Description:
  1. interfaces and classes are juxtaposed relationship
  2. interfaces can not be instantiated
  3. The interface can have constant and abstract method (before jdk1.8)
  4. The interface and its the relationship between: inheritance, but multiple inheritance
  relationship between classes and interfaces 5: class name of the class implements the interface, but also can implement multiple interfaces (Java is to achieve more than a single inheritance)
  polymorphism between classes and interfaces 6

usage interface classes:
  1. the same behavior can be achieved by unrelated class interface class, without regard to the hierarchical relationship between these classes
  2 through the interface may indicate the need to achieve a method of a plurality of classes, typically used to define the object extended function
  3. primarily used to define the interface specifications. Contacting coupling relationship

public  class InterfaceTest {
     public  static  void main (String [] args) { 
        
        Mouse Mouse = new new Mouse (); 
        
        Computer Computer = new new Computer (); 
        computer.useMouseBlue (Mouse); 
        computer.useMouseUsb (Mouse); 
        
        // Create Interface Object implementation class anonymous 
        the USB kB = new new the USB () { 
            @Override 
            public  void Start () {
                 // the TODO Auto-Generated Stub Method 
                System.out.println ( "normal connection keyboard" ); 
            } 
            
            @Override
            public  void End () {
                 // the TODO Auto-Generated Stub Method 
                System.out.println ( "Keyboard disconnected" ); 
            } 
        }; 
        
        computer.useMouseUsb (kB); 
        
        
        // create an anonymous anonymous object implementation class 
        computer.useMouseUsb ( new new the USB () { 
            
            @Override 
            public  void Start () {
                 // TODO Auto-Generated Stub Method, 
                System.out.println ( "good evening" ); 
            } 
            
            @Override 
            public  void End () {
                 //Auto-Generated Method Stub the TODO 
                System.out.println ( "See you tomorrow" ); 
            } 
            
        }); 
        
    } 
} 

class Computer { 
    
    public  void useMouseUsb (the USB USB) { 
        usb.start (); 
        System.out.println ( "normal connection " ); 
        usb.end (); 
    } 
    
    public  void useMouseBlue (BlueTooth BT) { 
        bt.startBlue (); 
        System.out.println ( " normal Bluetooth connection " ); 
        bt.endBlue (); 
    } 
} 

interface BlueTooth {
     void startBlue ();
     voidendBlue (); 
} 

interface the USB {
     void Start (); // default automatically added abstract public 
    void End (); 
} 

class Mouse the implements the USB, BlueTooth { 
    
    @Override 
    public  void Start () { 
        System.out.println ( "mouse start run " ); 
    } 
    
    @Override 
    public  void End () { 
        System.out.println ( " mouse stopped working " ); 
    } 
    
    @Override 
    public  void startBlue () { 
        System.out.println ( " start Bluetooth connected " );
    }
    
    @Override 
    public  void endBlue () { 
        System.out.println ( "disconnect the Bluetooth" ); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/jiujiang/p/11613891.html