The three-member study day11- Java class: constructor (constructor)

 

A constructor (the constructor)

Syntax:

Modifier class name (parameter list) { 
    initializer; 
}

  Constructor features:

    1. It has the same name as the class

    2. It does not declare the return type. (With different declared void)

    3. can not be static, final, synchronized, abstract, native modification, can not have a return statement returns the value

  The role of the constructor: create object 1; 2 to initialize the object; The Order o = new Order (); Person p = new Person (Shangxiang, 15);.

Example: create an instance of the class Animal: Animal a = new Animal (); call the constructor, is initialized to the legs 4.

public  class Animal {
     Private  int legs;
     public Animal () {= 4 legs;} // constructor to call a constructor initializes the legs 4 of 
the front face (the default constructor with no modifier has defined classes related to access, public class is the default constructor is public, the default class is used by default, and default constructor)
public void setLegs ( int I) { legs ' = I; } public int getLegs () { reyurn legs'; } }

  Depending on the parameters, the configuration may be divided into two categories:

    1. No implicit argument constructor (default provided)

    2. Define one or more display configurations (no reference, there is reference)

  Note: 1.JAVA language, every class has at least one constructor

        2. The default configuration is consistent with the modifier belongs to the class of modifiers

     3. Once defined configuration display, then the system does not provide a default constructor

     4 can create a plurality of class constructor overloads

     The parent class constructor is not inherited by subclasses

 

Example 1: Add Person class constructor defined above, the configuration using the set initial value of the property age are all 18.

package day11;

public class Person1 {
    public Person1(){
        age = 18;
    }
    public int age;
}



package day11;

public class test1 {
        public static void main(String[] args){
    Person1 p1 = new Person1();
    System.out.println(p1.age);
        }
}

Run results: 18

Example 2: Modify the title class constructor and increase the name attribute, attribute value such that the age and the attribute name is created each time the Person object also initializes the object.

package day11;

public class Person1 {
    public Person1(int a,String n){
        //age = 18;
        age = a;
        name = n;
    }
    public int age;
    public String name;
}



package day11;

public class test1 {
        public static void main(String[] args){
    //Person1 p1 = new Person1();
    //System.out.println(p1.age);
        Person1 p1= new Person1(15,"孙尚香");
        System.out.println(p1.age);
        System.out.println(p1.name);
        }
}

Run results: 15

      Shangxiang

Example 3: define a "point" (Point) class is used to represent a point in three-dimensional space (three coordinates). Requirements are as follows:

    1 may be generated having a specific coordinate point object.

    2. Providing a method may be provided three coordinates.

package day11;

public class Point {
    public Point (int m,int n,int k){
        x = m;
        y = n;
        z = k;
    }
    int x;
    int y;
    int z;
    
    public void setPoint(int m,int n,int k){
        x = m;
        y = n;
        z = k;
    }
    
} 



Public  class test1 {
         public  static  void main (String [] args) { 
        Point PO = new new Point (. 1, 2,. 3); // generate a specific coordinate point objects 
        po.setPoint (. 6,. 7,. 8); // Set m, n, k is 6,7,8 
        } 
}

Second, the constructor overloads

  General constructor to initialize the object while the object is created. Such as

class Person{
    String name;
    int age;
    public Person(String n, int a){
        name = n;
        age = a;
    }
}

  Overloaded constructor to create the object was more flexible, easy to create a variety of objects. Example constructor overload:

public  class the Person {
     public the Person () {// constructor overload, must be different parameter list 

    } 
    public the Person ( int Age) { 

    } 
    public the Person (String name) { 
   
    } 
    public the Person ( int Age, String name) { 
    
    } 
}

Three, this keyword

  In Java, the role of this keyword and its meaning is very close, mainly:

    1.this keyword inside a method, that is, its role and its meaning is very close.

    2. It is used in the constructor, which represents the object constructor being initialized.

  this represents the current object, call the class attributes, methods, and constructors. When the need to use an object method calls inside the method, use this. E.g:

Package Day12; 

public  class the Person {
         Private String name;
         Private  int Age;
         public the Person (String name, int Age) {
             the this .name = name; // when shaped member variables involved in the same name, if necessary using internal methods member variables , 
            this .age = Age; // must be added to indicate that this is a class member variable 
        } 
        
        public  void the setName (String name) {
             this .name = name; // in any method, if the current class member variable or member the method may be added in front of this, a program to enhance readability 
        } 
        
        public  void setName01 (String name) {
            this.setName(name);
        }
        
        public void showInfo(){
            System.out.println("姓名:" + name);
            System.out.println("年龄:" +this.age);
        }
    }    
 

  Use of this class calls this constructor (this special format as a class, with each call of the constructor)

Package Day12; 

public  class of Person1 {
     Private String name;
     Private  int Age;
     public of Person1 () { // constructor with no arguments 
        System.out.println ( "new object instantiated" ); 
    } 
    public of Person1 (String name) {
         the this () ; // call the constructor with no arguments for this class 
        the this .name = name; 
    } 
    public of Person1 (String name, int Age) {
         the this (name); // call a constructor parameter of 
        the this .age = Age; 
    } 
    publicGetInfo String () {
         return "Name:" + name + ", Age:" + Age; 
    } 
}

  Note: 1 using this () constructor must be placed on the first line!

     2. Use this call other constructor in this class, ensure that at least a constructor is not of this. (In fact, can not appear constructor calls itself).

Four, JavaBean

  1.JavaBean is a language written in Java reusable components.

  2. JavaBean refers to the so-called Java class meet the following criteria:

    > Class is public

    > Has no parameters a public constructor

    > Has an attribute value, the attribute is generally private and a corresponding get, set method

  3. The user can use JavaBean to function, processing, value, database access, and any other objects that can be created in Java code packaged, and other internal developers can JSP page, Servlet, other JavaBean, applet or application program to use these objects. Users may think JavaBean provides one kind of copy and paste functionality anytime, anywhere, without concern for any change.

Package Penalty for Day12;
 / ** 
 * A complete JavaBean 
 * public class 
 * private property 
 * attributes corresponding get and set methods 
 * * / 
public  class Person2 {
     Private String name; // Name 
    Private  int Sex; // Sex Male 0 1 female 
    Private  int Age; // Age 
    
    public  void the setName (String name) {
         the this .name = name; 
    } 
    
    public String getName () {
         return  the this .name; 
    } 
    
    public  void setSex (int sex){
        this.sex = sex;
    }
    
    public int getSex(){
        return sex;
    }
    
    public void setAge(int age){
        this.age = age;
    }
    
    public int getAge(){
        return this.age;
    }
}




package day12;
/**
 * JavaBean的用法
 * **/
public class test {
    public static void main(String ages[]){
        Person2 p2 = new Person2();
        
        p2.setName("杜甫");
        p2.setSex(0);
        p2.setAge(20);
        
        System.out.println(p2.getName());
        System.out.println(p2.getSex());
        System.out.println(p2.getAge());
    }
}

Print results: Du Fu

      0

      20

  Tip: use Eclipse automatically generates get and set methods

      After written property, in the code right mouse button, find the Source (shortcut Alt + Shift + S) option, and then select Generate Getters and Setters ..., then select the attributes you want to generate get and set methods, click ok carry out.

Guess you like

Origin www.cnblogs.com/su-peng/p/11908512.html