The basic characteristics of the object-oriented Java

Object oriented basic features:

1, the package

2, inheritance

3, polymorphism

Package

1, the benefits:

(1) hide implementation details, user-friendly use

(2) security, the visible range can be controlled

2, how to package?

By permission modifier

Permission modifier may be modified: class (classes, interfaces, etc.), properties, methods, constructors, class internal

Class (external class): public default and

Properties: four kinds

Methods: four kinds

Constructor: 4 kinds

Internal categories: four kinds

Javabean standard wording

class Student {public
    // property privatization
    Private String name;
    Private int Age;
    Private Boolean Marry;
    
    // common GET / SET
    public void the setName (String name) {
       this.name = name;
    }
    public String getName () {
        return this.name;
    }
    public void the setAge (int Age) {
        this.age = Age;
    }
    public int getAge () {
        return this.age;
    }
    public void setMarry (Boolean Marry) {
       this.marry = Marry;
    }
    public Boolean isMarry () {// get the type of boolean attribute method, get into the habit of using the iS
        return this.marry;
    }
}

Constructor

1, the constructor function:

(1) and used to create new objects with the
class name object name = new class name ();
class name object name = new class name (argument list);

(2) can be assigned as a property while creating an object

public class Circle{
    private double radius;
    public Circle(){ 
    }
    public Circle(double r){
        radius = r;//为radius赋值
    }
}

2, the statement constructor syntax:

[Modifier] {class name of the class
    [] Modifier class name () {// constructor with no arguments
        
    }
    [Modifier] class name (parameter list) {// configured with a reference
        
    }
}

3, constructed with the features:

(1) All classes have constructor

(2) If a class has no explicit / explicitly declare a constructor, the compiler will automatically add a default constructor with no parameters

(3) If a class explicit / explicitly declared constructor, then the compiler will not automatically add a default constructor with no arguments, if needed, then you need to manually add

(4) the name of the structure must be the same class name

(5) does not return type constructor

(6) can be overloaded constructor

Sample code:

public class Circle{
    private double radius;
    
    public Circle(){
        
    }
    public Circle(double radius){
        this.radius = radius;
    }
}

Guess you like

Origin blog.csdn.net/Brevity6/article/details/90676741