And object-oriented class Java--

A, classes and objects

Object-oriented program consists of objects, each containing a specific functional part of the user to achieve open and hidden part of
the problem is decomposed into smaller-scale process development approach ideal, object-oriented and more suitable for solving large-scale problems

1.1 Class

Construct the object class is a template or blueprint for
a process called class constructor object instance of the class is created
all the code written in Java are located inside a class

Package

  • Formally, the package is a combination of actions and data in a packet, and objects hidden user use data
  • Examples of process data objects is called a domain, referred to as a method for manipulating data
  • The key to the package must not be allowed that the methods in the class directly access instances of another class field, only the application interacts with the object by the process data objects
  • A class can change the way data is stored in a comprehensive manner, as long as still using the same method of operation data, other objects will not know or care changes that occur

inherit

  • To create another process by extending a class a class called inheritance
  • New class after having extended all the attributes and methods of the extension class

1.2 Object

The three main characteristics of the object

  • Behavior of an object - what operations can be applied to objects
  • State of the object - those applied when, how objects respond
  • Object identification - how to identify with the same behavior and state of different objects

Each object holds information describing the current features, namely the state of the object
change the object's state must be achieved by calling the method, or only show package was compromised

The relationship between class 1.3

The most common class of the relationship between the dependence of polymerization, inheritance
1 dependent
"use-a" relationships, is a clear, the most common relationship
if a class method manipulation object of another class, say class dependent in another class
should be possible to minimize the interdependence of the class, so that the minimum degree of coupling between the class
2, the polymerization
is also called "association"
, "a-has" relationship
object class a, class B comprises the object
3, Inheritance
"is-a" relationship, represents a special relationship with the general

1.4 class

1, initialization
of local variables within a function, the compiler will not directly to the default value, needs to be initialized before use. But the class member variables, the compiler will default value, can be used directly
after a new target, internal property values initialized by default:

  • short/int/byte:0
  • long:0L
  • boolean:false
  • char:'\u0000'
  • float:0.0f
  • double:0.0d

2, the assignment
substantially the value of variable assignment is copy
the object pointer assignment are assigned directly, two objects point to the same place in memory

package OO;

/**
 * Java类与对象
 * @version 15:40 2019-09-28
 * @author xxwang1018
 */

class Father {
    private int money = 100;  //私有
    long mobile = 13999999999L;

    public void hello() {
        System.out.println("hello");
    }
}
class Son extends Father {
    public void hi() {
        //子类可以扩展自己的成员方法
        System.out.println("hi~~~~~");
    }

    public void s1()    {
        Son s = new Son();
        System.out.println(s.mobile); //Son没有定义mobile, 而是通过父类继承的
        //System.out.println(s.money); //error 父类的money是私有的,子类无法直接访问
        //子类必须通过父类的方法才能访问父类的私有变量
        s.hello();  //Son没有定义f1, 而是通过父类继承的
        s.hi();     //Son可以自定义自己的成员方法
    }
}

public class Class_Object {
    private int a;
    public void setA(int a) {
        this.a = a;
    }
    public int add(int b) {
        return this.a + b;
    }
    public static void main(String[] a) {
        int b = 5;
        Class_Object obj = new Class_Object();
        obj.setA(10);
        System.out.println(obj.add(b));

        Son son=new Son();
        son.s1();
    }
}

Second, a constructor

Role: to produce an object member variable assignment

  • Java constructor must be the name of the class name, and no return value
  • Each Java class must have a constructor
  • A class can have multiple constructors, as long as the parameter list are not identical to

  • When the new object, depending on the argument, the automatic selection of the appropriate constructor. If the argument is not a matching parameter will be given
  • Java has automatic memory recovery mechanism, when the variable out of its life cycle, JVM will automatically reclaim the allocated memory object

initialization

1, the default field is initialized
if the constructor does not explicitly initial value to a domain (with a constructor), automatically assigned default value: the value is 0, the Boolean value is false, null object reference
2, the relevant configuration parameters is
if the constructor is not explicitly defined, Java compiler automatically generates an empty argument constructor for the class invisible, all instance fields set to the default value
if there has been an explicit argument constructor, the compiler no operation
if class provides at least a constructor, but did not provide a no-argument constructor, when the object is constructed If no parameters will be considered illegal, as a general matter the prevention of hand-code a no-argument constructor, for example,

new Employee(){
    //no information
}

3, the constructor call another
if the first statement of the this type constructors, such as (...), the constructor calls another of the same class constructor (which depends on the particular call parameters match)

Call the constructor of step

  1. All data fields are initialized to default values
  2. The order in the class declaration occurs sequentially performs initialization statements and all domains initialization block
  3. If the processor of the first row of the second constructor call, the second body is configured to execute
  4. Implementation of body structure
import java.util.*;
 
/**
 * 对象构造
 * @version 08:09 2019-09-30
 * @auther xxwang1018
 */
 
public class ConstructorTest{
    public static void main(String[] args){
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("harry", 40000);
        staff[0] = new Employee(60000);
        staff[0] = new Employee();
        
        //print out information about all Employee objects
        for(Employee e : staff)
            System.out.println("name="+ e.getName()+ ",id="+ e.getId()+ ",salary="+ e.detSalary());
    }
}
 
class Employee{
    private static int nextId;
    
    private  int id;
    private  String name = ""; //intance field initialization
    private double salary;
    
    //static initialization block
    static{
        Random generator = new Random;
        //set nextId to a random number between 0 and 9999
        nextId = generator.nextInt(10000);
    }
    
    //object initialization block
    {
        id = nextId;
        nextId++;
    }
    
    //three overloaded constructors
    public Employee(String aName, double aSalary){
        name = aName;
        salary = aSalary;
    }
    
    public Employee(double aSalary){
        //calls the Employee(String, double) constructor
        this("Employee #"+ nextId, aSalary);
    }
    
    //the default constructor
    public Employee(){
        //name initialized to "" --see above
        //salary not explicitly set --initialized to 0
        //id initialized in initialization block
    }
    
    public String getName(){
        return name;
    }
    
    public double detSalary(){
        return salary;
    }
    
    public int getId(){
        return id;
    }
}

Third, information hiding, and this

Object-oriented have a rule: information hiding

  • Members of the class are private property private
  • The method is public class public, modify the value by means of the member properties

Analogy: familiar friend, it will not directly take things into his drawer, but accessed through his public interfaces, revising what
this role

  • Pointing member variables in this class
  • Method according to member class this.add(5,3); //调用本类的add方法, this可忽略
  • Instead of this class constructor this(5); //调用本类的只有一个形参的构造函数
public class ThisTest {
    private int id;
    
    public ThisTest(int id) {
        this.id = id; //指向本类中的成员变量
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

Guess you like

Origin www.cnblogs.com/xxwang1018/p/11610853.html