java-Classes and objects

Source: Internet
Author: User

    • Classes and objects
    1. A class is the type of an object and is a collection of objects that have the same properties and methods.
    2. Defining classes
      • Access modifier Class class name {
      • Data Type property name;
      • Access modifier return value type method name {}
      • }
    3. Create object: Class Name Object name = new class name ();
    4. Working with objects: referencing the properties of an object: Object name, property name, method to reference object: Object name. Method Name ();
    5. You can create multiple objects of that class based on a class, each of which has its own members and is independent of each other.
    • member variables and local variables
    1. Member variables: defined in a class that describes what the object will be. Can be used in methods of this class or in methods of other classes. Java gives the member variable an initial value.
    2. Local variables: defined in a method of a class to temporarily save data in a method. Can only be used in the current method. Java does not assign an initial value to a local variable.
    3. When two types of variables have the same name, local variables have higher precedence.

Telephone.java

 Package com.test;  Public class Telephone {    double screen ;     Double CPU;     Double mem;      Public void SendMessage () {        System.out.println ("screen:" + screen + "; Cup:" + CPU + "; Mem:" + mem);}    } 

Demo.java

 Package com.test;  Public class Demo7 {    publicstaticvoid  main (string[] args) {        new  Telephone ();        Phone.sendmessage ();         = 5.4;         = 1.2;         = 6.0;        Phone.sendmessage ();    }}

Output Result:

screen:0.0;cup:0.0;mem:0.0
screen:5.4;cup:1.2;mem:6.0

    • Construction method
    1. The previous definition of an object is not entirely correct, it should be the new + constructor method.
    2. The constructor method is to define a method in the Java class that initializes the object.
    3. The constructor method has the same name as the class, and there is no return value.
    4. When no constructor method is specified, the system automatically adds a parameterless construction method
    5. A parameterless construction method is not automatically added when there is a specified construction method (whether it is a constructor with parameters or no parameters)
    6. Overloading of constructor methods: multiple methods with the same method name but different parameters, automatically selecting the appropriate method according to different parameters
    7. The construction method can not only assign a value to an object, but also guarantee a reasonable value to the property of the object.

Telephone.java

 Packagecom.test; Public classTelephone {DoubleScreen ; DoubleCPU; DoubleMem;  Publictelephone () {System.out.println ("No parameter construction method"); }     PublicTelephone (DoubleNewscreen,DoubleNEWSPU,DoubleNewmem) { screen=Newscreen; CPU=NEWSPU; Mem=Newmem; System.out.println ("Constructor method with parameters"); }     Public voidSendMessage () {System.out.println ("Screen:" + screen + "; Cup:" + CPU + "; Mem:" +mem); }}

Demo.java

 Package com.test;  Public class Demo {    publicstaticvoid  main (string[] args) {        new telephone ();         = 5.4;         = 1.2;         = 6.0;        Phone.sendmessage ();                 New Telephone (5.0, 1.5, 4.0);        Phone1.sendmessage ();    }}

Operation Result:

method of constructing without parameters

screen:5.4;cup:1.2;mem:6.0

A method of constructing parameters

screen:5.0;cup:1.5;mem:4.0

    • static variables

Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. When the system uses the class for the first time, it allocates memory space until the class is unloaded for resource recycling!

Static members can be accessed directly by using the class name or by using the object name. Of course, given the specificity of his role, it is more recommended to access it with a class name.

    • Static methods

The method of static modification is known as a stationary method or a class method.

    1. Static members and static methods in the same class can be called directly in a statically method, but non-static members and static methods cannot be called directly. If you want to invoke non-static and non-static methods in a static method, you can access both non-static and non-static methods by creating the object of the class and then through the object.
    2. In the normal member method, the same non-static variable and static variable can be accessed directly.
 Packagecom.test; Public classDemo8 { Public StaticString Classroom = "Java class";  PublicString Schoolname = "Hope Primary School";  Public Static voidGetName () {System.out.println ("=======getname=======");        SYSTEM.OUT.PRINTLN (classroom); Demo8 Demo=NewDemo8 ();    System.out.println (Demo.schoolname); }         Public voidfunc () {System.out.println ("=======func=======");        SYSTEM.OUT.PRINTLN (classroom);    System.out.println (Schoolname); }     Public Static voidMain (string[] args) {System.out.println ("=======main=======");        System.out.println (Demo8.classroom);        SYSTEM.OUT.PRINTLN (classroom);        Demo8.getname ();        GetName (); Demo8 Demo=NewDemo8 ();    Demo.func (); }}

Execution Result:

=======main======= Java class Java class =======getname=======Java class Hope Primary School =======getname=======Java class Hope Primary School =======func=======Java class Hope Primary School
    • Initializing blocks for data assignment

In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you initialize a block with the static adornment, it is called a static initialization block.

Note: Static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables.

  

 Packagecom.test; Public classDemo9 {intAge ;    String name; StaticString sex;  PublicDemo9 () { age= 12; System.out.println ("Initialize Age by constructor"); } {Name= "Java"; System.out.println ("Initialize name by initialization block"); }    Static{Sex= "female"; System.out.println ("Initialize sex by static initialization block"); }         Public Static voidMain (string[] args) {Demo9 demo=NewDemo9 (); System.out.println ("Age" + demo.age + "name" + demo.name + "gender" +sex); Demo9 Demo1=NewDemo9 (); }}

Execution Result:

Initializing a sex by initializing a block initialize the name through the initialization block initializes the age 12 name through the constructor initializes the name of the

When the program runs, the static initialization block is executed first, then the normal initialization block is executed, and the construction method is finally executed. Because static initialization blocks are only executed once when the class is loaded, static initialization blocks are not executed when the object Demo2 is created.

  

java-Classes and objects

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.