CGBTN2109-DAY10 summary review

DAY10 review

1. Interface

  1. We define the interface through the interface keyword
  2. The interface implementation class needs to establish the implementation relationship with the interface through the keyword implements
  3. Interface cannot create object / instantiate
  4. In Java 8, all methods in the interface are abstract methods. Methods can be abbreviated and will automatically splice public abstract
  5. There is no constructor in the interface. The constructor called by the interface implementation class is the parent class, not the parent interface
    Note: if a class does not explicitly specify a parent class, it will inherit the top-level parent class Object by default
  6. There are no member variables in the interface. They are static constants. public static final will be spliced by default
  7. Interface is not a class!!! You can understand the interface as a special abstract class. In particular, all methods are abstract methods
  8. If an implementation class implements an interface, it will either become an abstract subclass [do not implement / implement part], or become an ordinary subclass [implement all]
  9. The interface is used to formulate rules [what methods are there, do the methods have parameters? Do they have return value types?], and it is not implemented in detail
  10. Interfaces can inherit multiple interfaces [one interface can inherit multiple interfaces] multiple implementations [one class can implement multiple interfaces]

2. Production of standards

1. Member variable - attribute

private is used for encapsulation. After encapsulation, corresponding getXxx() and setXxx() are provided

2. Construction method

  1. Provides a parameterless construct of this class
  2. Provide a full parameter Construction: create object + assign values to all attributes of the object

3. Membership method - General method

  1. Some methods are provided according to the business of this class
  2. Provide a method to display the class name of this class and all properties and property values of this class

4. Create objects for testing

  1. Create an object using a parameter free structure, and then call setXxx() to assign all attributes to the object.
  2. Create an object with full parameter construction, and the properties of the object also have values

5. Practice consolidation

Requirements: create a student class and test it according to the above steps
Student attributes: student number name gender address subject
Students' function: learn to eat and sleep

  1. Create Student class Student
package cn.tedu.oop;
/*This class is used to describe students*/
public class Student {
    //1.1 defining student attributes
    //1.2 encapsulating student attributes
    private int id;//Student number
    private String name;//full name
    private char gender;//Gender
    private String address;//address
    private String subject;//subject

    //2.1 provide the parameterless structure of this class
    public Student(){
        System.out.println("I am Student Nonparametric structure of");
    }
    //2.2 provide full parameter structure of this class
    //Right click - > generate - > constructor - > shift to select all attributes - > OK
    public Student(int id, String name, char gender, String address, String subject) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.address = address;
        this.subject = subject;
        System.out.println("I am Student All parameter structure of");
    }
    //3.1 general methods of providing this class 1
    public void study(){
        System.out.println("Learning makes me happy~");
    }
    //3.2 provide common methods of this class 2
    public void eat(int n){
        System.out.println("I'm going to do it today"+n+"Bowl of rice");
    }
    //3.3 provide common methods of this class 3
    public String sleep(){
        return "I had another good sleep today";
    }
    //4. Provide a method of this class to display types and all attributes and attribute values
    public void show(){
        System.out.println("Student:[id="+id+",name="+name+",gender="+gender+",address="+address+",subject="+subject+"]");
    }

    //1.3 provide get and set methods corresponding to attributes
    //Right click - > generate - > getter and setter - > shift to select all attributes - > OK
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}
  1. Create test class TestStudent for student class
package cn.tedu.oop;
/*This class is used as a test class for students*/
public class TestStudnet {
    public static void main(String[] args) {
        //1.1 creating objects using parameterless constructs
        Student s = new Student();
        //1.2 assign values to all attributes of the object through setXxx()
        s.setId(1);
        s.setName("Lin Chong");
        s.setGender('male');
        s.setSubject("CGB");
        s.setAddress("Temple");
        //1.3 test through object calling method
        s.study();
        s.eat(3);
        System.out.println(s.sleep());
        s.show();//Student:[id=1,name = Lin Chong, gender = male, address = temple, subject=CGB]

        //2.1 creating objects with full parameter construction
        Student s2 = new Student(2,"Li Kui",'male',"small pork-butcher's shop","ACT");
        s2.study();
        s2.eat(10);
        System.out.println(s2.sleep());
        s2.show();
    }
}

Keywords: Java interface

Added by synstealth on Mon, 18 Oct 2021 06:30:21 +0300