Java Learning notes, classes and objects

Source: Internet
Author: User


Object oriented

    1. Object: Everything Objects

    2. Object oriented

    3. Class: Die, Attribute + method, class is the type of object, is a collection of objects with the same properties and methods

    4. Object properties: Objects have a variety of characteristics, "What is the object?"

    5. Object method: The action performed by the object, "what can the object do?"

    6. Class-to-object relationships/differences: Classes are abstract, just templates; objects are tangible entities that can be seen and touched. For example: ' Phone ' class, object is Iphone6,lumia920

Classes in Java

  1. Defining classes: Java programs use class classes as organizational units

    Create an Object

    1. Definition of a class

    public class telphone{

    2. What are the attributes (member variables)

    float screen;

    float CPU;

    float mem;

    3. How to Do

    void Call () {

    SYSTEM.OUT.PRINTLN (' telephone has the function of calling ');

    }

    }


  2. Steps for working with objects

    1. Create objects: Classes are placed in the form of code in a file, instantiating new objects in memory

    Class Name Object name = new class name (); Class instantiation becomes object

    Telephone phone = new Telphone ();

    650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650; src=/e/ U261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>650" this.width=650; "src="/e/u261/themes/default/ Images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px Solid #ddd; "alt=" Spacer.gif "/>650) this.width=650; src=" http://s3.51cto.com/wyfs02/M00/4B/CD/ Wkiol1qz4gjqfmyyaagdldiqeze813.jpg "title=" 1.png "width=" "height=" 252 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:252px; "alt=" wkiol1qz4gjqfmyyaagdldiqeze813.jpg "/>

    2. Working with objects

    Reference object's properties: Object name. Properties

    Phone.screen = 5;

    Methods for referencing objects: Object names. Method Name ()

    Phone.sendmessage ();

    For example:

    public class initaltelphone{

    public static void Main (string[] args) {

    Creating objects

    Telphone phone = new Telphone ();

    Phone.sendmessage ();

    Assigning a value to an instance variable

    Phone.screen = 5.0f;

    PHONE.CPU = 1.4f;

    Calling Object methods

    Phone.sendmessage ();

    }

    }

member variables and local variables in Java

    1. Member variable: defined in a class to describe what the object has.

    2. Local variable: defined in a method of a class to temporarily save data in a method.

    3. Difference:

      1. Scope differs

          local variable: Only the method that defines it

          member variable: Inside the entire class is visible

      2. Different initial values

         java will give the member variable an initial value of

         java does not give the local variable the initial value

      3. In the same method, local variables with the same name are not allowed

          in different methods, You can have local variables of the same name

      4. Local variables have precedence when the member variable or local variable has the same name

         

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/4b/cd/wkiol1qz4nbdccmraakc2pnyyiu736.jpg "title=" 2.png "width=" height= "239" border = "0" hspace= "0" vspace= "0" style= "WIDTH:450PX;HEIGHT:239PX;" alt= "wkiol1qz4nbdccmraakc2pnyyiu736.jpg"/>

Construction method

  1. Create a new object using the new+ construction method

  2. A constructor is a method that is defined in a Java class to initialize an object.

    The constructor method has the same name as the class and no return value

    650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/4b/cd/wkiol1qz4rjwyd2zaacd7l_4fl0502.jpg "title=" 3.png "alt=" wKioL1Qz4RjwYd2zAACd7L_ 4fl0502.jpg "/>

  3. Use of the non-parametric construction method

    Telphone class:

    public class telphone{

    float screen;

    float CPU;

    float mem;

    Public Telphone () {

    System.out.println (' Non-parametric construction method executed! ‘)

    }

    Public Telphone (float newscreen,float newcpu,float newmem) {

    Construction method assigns a reasonable value to an object's properties

    if (newscreen<3.5f) {

    SYSTEM.OUT.PRINTLN (' The parameter you entered has a problem, automatically assigns a value of 3.5 ');

    screen = 33.5f;

    }else{

    screen = Newscreen;

    }

    CPU = NEWCPU;

    MEM = Newmem;

    }

    }

    The entry class Initaltelphone, which contains the entry function main ():

    public class initaltelphone{

    public static void Main (string[]) {

    Creating an object by using an argument-free construction method

    Telphone phone = new Telphone ();

    You can also create an object with an argument constructor, and assign an initial value to an instance variable in the object

    Telphone Pho2 = new Telphone (5.0f,1.4f,2.0f);

    }

    }

  4. When no construction method is specified, the system automatically adds an argument-free construction method

  5. When there is a specified construction method, no parameter is automatically added to the construction method, no matter whether it is a parameter or no argument

  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 values to the properties of an object, but also guarantee a reasonable value to the object's properties.

Use of static

  1. Static variable used by static: a member that is statically decorated is called a static member or a class member. It is owned by the entire class, not by an object, which is shared by all objects of the class.

    Static members can be accessed directly using the class name or accessed using the object name: 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" spacer.gif "/>

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4B/CB/wKiom1Qz4RWjcLnkAAPqaAyc14U581.jpg "title=" 4.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:450px;height:250px;" alt= " Wkiom1qz4rwjclnkaapqaayc14u581.jpg "/>

  2. Static methods used by static: static modification methods, referred to as statically or class methods

    The use of static methods:

    650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/4b/cd/wkiol1qz4yjwzfanaamrlkzhlii465.jpg "title=" 5.png "width=" height= "296" border = "0" hspace= "0" vspace= "0" style= "width:450px;height:296px;" alt= "wkiol1qz4yjwzfanaamrlkzhlii465.jpg"/>

    Example: In the editor, two static variables score1 and score2 are defined respectively to save the score information. Defines a static method sum, which calculates the score score, and outputs the score score in the main method by calling the static method

    public class HelloWorld {

    Defining Static Variables Score1

    static int score1 = 86;

    Defining Static Variables Score2

    static int score2 = 92;

    Defines the static method sum, calculates the score score, and returns the total score

    public static int sum () {

    return score1+score2;

    }

    public static void Main (string[] args) {

    Call the static method sum and receive the return value

    int allscore = Helloworld.sum ();

    SYSTEM.OUT.PRINTLN ("Total score:" + Allscore);

    }

    }

  3. Static initialization blocks used by static: data can be assigned by initialization blocks in Java. It is important to note that static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables. Static members are owned by the entire class, not by an object, and access to static members can use either the class name or the object name.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/4b/cb/wkiom1qz4z3j49zeaaqqw4u4aqc376.jpg "title=" 6.png "width=" height= "338" border = "0" hspace= "0" vspace= "0" style= "width:450px;height:338px;" alt= "wkiom1qz4z3j49zeaaqqw4u4aqc376.jpg"/>


This article is from the "Moonlight into Ashes" blog, please be sure to keep this source http://ankneo.blog.51cto.com/9446030/1561065

Java Learning notes, 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.