Java-encapsulation, inheritance

Package

Object-oriented encapsulation: encapsulation can improve security, encapsulate attributes and methods into a type, attributes are decorated with private, improve the security of attributes.
Encapsulation is easy to use, the property is decorated with private, and the method is decorated with public. When accessing private properties (private modified properties), you can access them through public methods in this class (public modified methods)

public  class Test1 {
     public  static  void main (String [] args) { 
        User user = new User ();
 //         user.name = " 
        guanyu "; // error user.setName ("关羽" ); 
    } 
} 
class User {
     // The property and method are encapsulated together through a class. The property is decorated with private and the method is decorated with public. The outside world cannot directly access the property, but the private property can be accessed indirectly through the public method 
    private String name;
     public  void setName (String name) {
         this .name = name; 
    } 
}

javaBean

It is a customary habit developed by enterprises, not a specific grammatical concept, used to save data.
javaBean requires:
1) The property is private (use private to modify the property)
2) There must be a publicly decorated get / set method to access the private property
3) The construction method must have, and there must be a null parameter construction method
Purpose: to manipulate data, Easy to use

public class Test1 {
    public static void main(String[] args) {
        Person person=new Person();
        person.setName("刘诗诗");
        person.setAddress("中国");
        person.setAge(29);
        person.setPhone("12355566666");
        System.out.println(person.getName());
        System.out.println(person.getAddress());
        System.out.println(person.getAge());
        System.out.println(person.getPhone());
    }
}
class Person{
    private String cardNo; // private attribute 
    private String name;
     private String phone;
     private  int age;
     private  double salary;
     private String address;
     public Person () {} // construction method of empty parameter
     // for private attributes, there is get / set method to access 
    public String getCardNo () {
         return cardNo; 
    } 
    public  void setCardNo (String cardNo) {
         this .cardNo = cardNo ; 
    } 
    public String getName () {
         return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

inherit

1) Reuse: development only needs to define unique functions (attributes and methods), and then reuse the functions (attributes and methods) that have been written
2) extends: inherited keyword
format: class subclass extends parent class
3) Subclasses can inherit the methods and properties of the parent class
4) The essence of inheritance: copy the properties and method codes of the parent class to the subclass, and then write the subclass code, you can reduce the amount of code, only need to add a new part
5) java Inheritance in single inheritance: subclasses can only have one direct parent class, but layers of inheritance

public  class Test1 {
     public  static  void main (String [] args) { 
        Student student = new Student ();
         // name, age, sex these three attributes, Student class inherits from parent class Person2 
        student.name = " Liu Yingqian " ; 
        student.age = 18 ; 
        student.sex =" Male " ; 
        student.address =" Harbin " ; 
        student.score = 100 ; 
        student.speak (); 
        student.add ( 12, 23 ); 
        
        Teacher teacher = new Teacher ();
        teacher.name age;= "Kong Xiangyan" ; 
        teacher.age = 18 ; 
        teacher.sex = "Female" ; 
        teacher.salary = 5000 ; 
        teacher.speak (); 
        teacher.teach (); 
    } 
} 
// Design a Person class, attribute: name, Gender, age
 // don't write any construction method at the moment, method: speaking behavior
 // design a Student class, attributes: name, gender, age, score, address
 // don't write any construction method at the moment, method: speaking Behavior, addition
 // design a Teacher type, attributes: name, gender, age, salary
 // don't write any construction method for the time being, method: speaking behavior, lecture 
class Person2 { // parent class 
    String name;
     int 
    String sex ; 
    void speak () {
        System.out.println("我是:"+name);
    }
}
class Student extends Person2{//子类
    int score;
    String address;
    void add(int num1,int num2){
        int sum=num1+num2;
        System.out.println(sum);
    }    
}
class Teacher extends Person2{//子类
    double salary;
    void teach(){
        System.out.println("讲课");
    }
}
public class Test1 {
    public static void main(String[] args) {
        Foo4 foo4=new Foo4();
        System.out.println("f1="+foo4.f1);
        System.out.println("f2="+foo4.f2);
        System.out.println("f3="+foo4.f3);
        System.out.println("f4="+foo4.f4);
    }
}
class Foo1{
    int f1=1;
}
class Foo2 extends Foo1{
    int f2=2;
}
class Foo3 extends Foo2{
    int f3=3;
}
class Foo4 extends Foo3{
    int f4=4;
}

Use of construction methods in inheritance

1) The subclass cannot inherit the construction method of the parent class, the subclass will call the constructor of the parent class by default (there is no parameterless construction in the
parent class ) 2) There is a parameterless construction method in the parent class, the subclass can be arbitrarily designed Construct method yourself

public  class Test1 {
     // The parent class has an empty parameter constructor, then the subclass can create its own constructor arbitrarily 
    public  static  void main (String [] args) { 
        Emp2 emp = new Emp2 ("刘", 23 ) ; 
        emp.show (); 
        Emp2 emp1 = new Emp2 ("王", 37,30000.0 ); 
        emp1.show (); 
        System.out.println (emp1.salary); 
    } 
} 
class Emp1 { 
    String name; 
    int age;
     // The default constructor for null parameters is 
    void show () { 
        System.out.println (name + "," + age); 
    }
} 
class Emp2 extends Emp1 {
     double salary;
     // The parent class has a constructor with empty parameters, and the subclass can design its own constructor arbitrarily 
    Emp2 (String name, int age, double salary) {
         this .name = name;
         this .age = age;
         this .salary = salary; 
    } 
    Emp2 (String name, int age) {
         this .name = name;
         this .age = age; 
    } 
}

There is no empty parameter constructor in the parent class, then the constructor of the subclass must call the constructor with parameters in the parent class
super: past, once
super (parameter): written in the first line of the subclass constructor, Call the constructor in the parent class

public class Test1 {
    public static void main(String[] args) {
        User2 user=new User2("张","123456",20);
        System.out.println(user.name);
        System.out.println(user.password);
        System.out.println(user.age);
    }
}
class User1{
    String name;
    String password;
    //父类中构造方法带参数
    User1(String name,String password){
        this.name=name;
        this.password=password;
    }
}
class User2 extendsUser1 {
 // When there is only a constructor with parameters in the parent class, the subclass will call the constructor of the parent class by default.
 // Using super to call the constructor of the parent class
 // In enterprise development, the parent class is best Write a constructor with no parameters, so that subclasses can create their own constructor 
    int age; 
    User2 (String name, String password, int age) { 
        super (name, password); // super (parameters): Call the constructor in the parent class according to the parameters, and super must be written in the first line
         this .age = age; 
    } 
}
public  class Test1 {
     // General advice for enterprise development, when designing a class, add a constructor for empty parameters, so that other programs can arbitrarily design their own subclass construction method according to the parent class 
    public  static  void main (String [ ] args) { 
        Koo2 koo2 = new Koo2 ("Liu", 23, "Guangzhou" ); 
        System.out.println (koo2.name + "," + koo2.age + "," + koo2.address); 
    } 
} 
class Koo1 { 
    String name; 
    int age; 
    Koo1 (String name, int age) {
         this .name = name;
         this .age = age; 
    } 
    // Add a null parameter constructor to the parent class
    Koo1(){}    
}
class Koo2 extends Koo1{
    String address;
    Koo2(String name,int age,String address){
        this.name=name;
        this.age=age;
        this.address=address;
    }
}

Finish

Guess you like

Origin www.cnblogs.com/peiya/p/12691002.html
Recommended