Day7 Python learning object-oriented programming objects and classes 1-

First, the concept Java programming objects and classes

1. What is the class?

A: The class is an objective abstract concept of what exists.

2. What is the object?

Answer: The object is concrete and practical, representing a thing. For example: a car, automobiles, bicycles is his target.

Description of classes and objects: class is a template object, the object is a class of individuals

3. The class syntax

Modifier class class name {
      Property declaration;
      Method declaration;
}

Description: 1 modifiers public: class may be arbitrarily accessed

   2. Class} {text enclosed use

 

4. The class objects are created. In Java, use the keyword new to create a new object. Create an object requires the following three steps:

1. object declaration: class name of the object name; // declare an object, including the object name and object type.
2. Examples of the: object name = new class name (); new new action: allocate memory space.
3. Initialization: When you create an object using the new, calls the constructor to initialize the object.
It may be co-written as: class name = new class name object name ();
Example:
public  class the Person { // create a person can be accessed by any class
     // property declaration; also known as a member variable declarations. (Class member variables can be declared without initialization, there is a class member variable initial values, such as String default value is null, the default value is int 0) 
    String name; // declare attribute name 
    int Age; // declare member variables Age
     // method declarations; also called the function declaration. 
    public  void showname () { // declare a method showname () 
    System.out.println (name);
    }
    public  int showAge () { // declare a method showAge (), if there is a return value of the method, the method of the last line of the body must be returned corresponding to the data, using the return key, the data type and method definitions returned consistent, e.g. age return value, the method as above defined type int 
        return age;
    }
    // main 
    public  static  void main (String [] args) { // instance of the Person class, i.e. create a Person object 
    Person = per new new Person (); // declare a Person type variable called per, new Person () is an instance of the Person class 
    per.name = "Peng"; // to name objects per attribute assignment 
    per.showName (); // call the object's methods 
    per.age = 800; // to a subject per age attribute assignment 
    int A = per.showAge ();
    System.out.println(a);
    }
}    

 The output is:

 

 

 

 

Guess you like

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