Constructors 20.Java base _ objects

 

. 1  Package Pack1;
 2  public  class Student {
 . 3      Private String name;
 . 4      Private  int Age;
 . 5  
. 6      // constructor
 7      // If no constructor, the system will automatically give a no-argument constructor 
. 8      public Student () {
 . 9          the this .name = "" ;
 10          the this .age = 0 ;
 . 11      }
 12 is      public Student (String name, int Age) {
 13 is          the this .name = name;
 14          the this .age =age;
15     }
16 
17     public void show(){
18         System.out.println(name+","+age);
19     }
20 }
Package Pack1;
 // test student class 

public  class StudentDemo {
     public  static  void main (String [] args) {
         // create objects 
        Student S1 = new new Student (); 
        Student S2 = new new Student ( "NJT", 22 is ); 
        S1. Show (); //   , 0 
        s2.show (); // NJT, 22 is 
    } 
}

 

Guess you like

Origin www.cnblogs.com/NiBosS/p/11959906.html