Object-oriented use of classes

Source: Internet
Author: User

JAVA Object-oriented one, concept 1. What is an object?

①. The things that you can see and touch in life, that is, objects-all things are objects
②. The person, thing, or entity that appears in the problem.
③. Consists of attributes and behaviors.

Computers, cell phones, books, cups, bulbs, can all be objects.

2. What is a class?

class: is a collection of a bunch of objects that have the same properties and methods.

3. What is the relationship between classes and objects?

The ①. Class is the description of the object.
②. An object is an instance of a class.

Ii. objects, classes and Packages 1. Class A. Class in the program:
    1. The name of the class: the first letter should be larger, such as: Student.
    2. A member variable of a class: That is, the object contained in the class.
    3. The method of the class, that is, the function.
B. member variables of the class:
    1. Access modifiers: public, private, or protected. Default modifier when omitted
    2. Data type
    3. Member variable name: Name must be a valid identifier, end of semicolon
C. Methods of the class:
    1. Access modifiers
    2. return value
    3. Method name, must be a valid identifier
    4. Parameter list, which appears in parentheses
2. Create a class:

Classes in Java are declared with the keyword class.
Java source code files can contain only one common class (public), and the file name must be the same as the class name of the common class, with the extension ". Java".

Program Examples:
public class Student {    //初始值  //成员变量 ,全局变量    String name;    int  number;//0    double score;    long phone;    //向类中添加方法(函数):声明一个方法体data();    //static静态方法调用时,参数必须同样为static    public void data(){        long phone = 1111;//------声明一个局部变量 phone; 局部变量必须初始化一个值;        System.out.println(number+"号  姓名:"+name+" "+"电话:"+phone);//就近原则输出    }    public double fun(){        return this.number/2;    }  }
3. Constructors

constructor of the class: access modifier + function name.
1. The name must be the same as the class name;
2. Cannot declare return value, cannot return void.
have participate in the parameterless constructor: before the parameter constructor, put an parameterless constructor to prevent error.

Use this (), call the parameterless constructor, and put it on the first line of the method.
Make this (x, y) call the parameter constructor and put it on the first line of the method.
The constructor is called once for each instantiation.

public class Student {    String name;    long phone;//0    double score;    public void data(){    }    public Student(String name,long phone){        data();            ------------- 调用无参构造函数        this.name = name;        this.phone = phone;        long phone = 1111;        System.out.println(number+"号  姓名:"+name+" "+"电话:"+phone);    }    public void Student(String name){        data(name,phone);  ------------- 调用有参构造函数        System.out.println(number+"号  姓名:"+name);    
4. References to classes--instantiation

In a Java program, the object of the class is referenced by the new operator.

Student s;s = new Student();

Reference s points to an in-memory student object. The operator new allocates memory for the object, and then assigns the initial value to all member variables of the object. Thus, no garbage data is generated.
Instantiation is the creation of two entities in memory: a reference and an object.

the whole process of a program: referencing a class, instantiated by new, completed by the Classload ClassLoader, and converted to class type. The class is then transformed into a concrete object by a constructor function.

5. Accessing the properties and methods of an object

When you access the member variables and methods of an object, you must associate with an Object!
For example:

Student s = new Student();s.name = "张三";s.data();  
6.this references

Each object can use the This keyword to refer to itself.

public double fun(){    return this.number/2;}  

Whether we explicitly add this reference or not, the compiler automatically adds it.

7. Use of packages (folders)

Each class belongs to a package. The package name can be prefixed with the class name.
The main purpose of the package is two:
1. Associating a class organization
2. Provide a namespace for the classes in the package.

A reference to a package avoids naming conflicts for the same name class in two packages.

In order to explicitly declare a class in a package, the class will exist in the default package.

8. References to array classes
    1. Define and instantiate: An array class, an array of one of the classes of an object
    2. Memory allocation: The reference name of the class exists in the stack, pointing to the concrete class in the heap. And each class has a heap of arrays.
    3. Stack: All data is present in stacks and heaps.

Stack: holds all underlying data types, including reference names. Features: small space, first executed. Call all into the stack, the stack must be executed.
Heap: Holds all reference types. The memory addresses are placed in the heap. Features: Large space, slow running, the stack points to the heap.

Object-oriented use of classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.