Java class definition and use

/ *
Definitions * Class:
* class is used to describe things in the real world
*
* things:
* description of the properties of things
* behavior can do things
*
* and things like how the correspondence it?
* Class:
* member variables
* member method
*
* requirements: students write a class
*
* students things:
* attributes: name, age ...
* behavior: study, eating ...
*
* student category:
* member variables: name, age
* members methods: the study, eat
*
* member variables: definitions and variables we've learned is the same.
* Different location: outside the class, method
* initialization value: do not need to initialize the value
* member method: the definition is the same method and we've studied is.
* Remove the static keyword
* /

public  class Student {
     // member variables
     // Name 
    String name;
     // Age 
    int Age; 
    
    // member method
     // learning methods 
    public  void Study () { 
        System. OUT .println ( " study hard and make progress every day " ); 
    } 
    
    // eat method 
    public  void eAT () { 
        the System. OUT .println ( " learned to eat hungry " ); 
    } 
}
* Use a class, in fact, is the use of members of the class. (Member variables and member methods)
  * and we want to use a member of a class, you must first have an object class.
 * How can we have a class object of it? 
 * Create objects on it? 
 * How do we create objects? 
 * Format: class name object name = new new class name ();
  * How Object Access member yet? 
 *          Member variables: Object . name variable name
  *          members method: method name object name (...)
  * /
 public  class StudentDemo {
     public  static  void main (String [] args) {
         // format: class name = new class name object name (); 
        S = Student new new Student ();
         // System.out.println ( "S:" S +); // com.itheima_02.Student@193c0cf 
        
        //Direct output value of the member variable 
        System. OUT .println ( " Name: " + s.name); // null 
        System. OUT .println ( " Age: " + s.age); // 0 
        . System OUT .println ( " ---------- " ); 
        
        // to members of variable assignment 
        s.name = " Brigitte " ; 
        s.age = 28 ; 
        
        // output value of the member variable again 
        System. OUT .println ( " name: " + s.name);// Brigitte 
        System. OUT .println ( " Age: " + s.age); // 28 
        . System OUT .println ( " ---------- " ); 
        
        // call member methods 
        s.study (); 
        s.eat (); 
    } 
}

Output follows

 

Guess you like

Origin www.cnblogs.com/longesang/p/10973855.html