A standard class typically has the following four components

A standard class usually have the following four components:

  1. All member variables must be modified using the private key
  2. Write Getter / setter methods one pair for each member variable
  3. Writing a no-argument constructor
  4. Write a full-constructor parameters

Such a standard class, also known as Java Bean

Standard class: 
public class Student { 
    
    Private String name; // name 
    private int age; // Age 
    
    public Student () { 
        // constructor with no arguments  }  public Student (String name, int Age) {  this.name = name; the this = .age Age;} // constructor parameters have public String getName () {return name;} public void the setName (String name) {this.name = name;} // int name public getAge () {return Age;} void the setAge public (int Age) {this.age = Age; Age} // } // // public class ================== CaiNiao {public static void main ( String [] args) {Student stu1 = new
Student (); stu1.setName ( "rookie" ); stu1.setAge (18 ); System.out.println ( "Name:" + stu1.getName () + " , Age:" + stu1.getAge ()); // The second way student stu2 = new Student ( "legend", 28 ); System.out.println ( "name:" + stu2.getName () + " , Age:" + stu2.getAge ()); STU2 .setAge (22); // modified after System.out.println ( "name:" + stu2.getName () + " , Age:" + stu2.getAge ());}}

 

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11073989.html