Java-like and objects

Source: Internet
Author: User

1. Class-to-object relationships

We learn programming language, is to simulate the real world things, to achieve information. For example: to the supermarket to buy things billing system, to the bank to transact business system.

How do we express a real-world thing?

Attribute: Is the descriptive information of the thing

Behavior: Is what the thing can do

For example, students have a name, age and other properties, students have to learn, play games and sleep and other behaviors.

The basic unit of the Java language is the class, so we should put things in a class to embody.


2. Definition of classes and objects

Class: is a set of related properties and behaviors.

Object: Is the concrete embodiment of this kind of thing.


3. Example

public class student{    string name;    int age;     string address;        public void  study () {        system.out.println ("student Study");     }    public void eat () {         System.out.println ("The Student Eats");     }    public void sleep () {         system.out.println ("Student Sleeps");     }}public  class studenttest{    public static void main (String[] args ) {        student stu = new student ();         stu.name =  "haha";         Stu.age = 20;        stu.address =  "Jiangsu";                 stu.study ();         stu.eat ();         stu.sleep ();     }}


4. The difference between a member variable and a local variable

Different positions in the class

Outside the method in the member variable class

Inside a local variable method or on a method declaration

In-memory locations are different

Member Variable heap memory

Local variable stack memory

Different life cycle

The member variable exists as the object exists and disappears as the object disappears

Local variables exist with the invocation of the method and disappear as the call to the method finishes

Different initialization values

The member variable has a default initialization value

Local variables do not have default initialization values, they must be defined, assigned, before they can be used

"Caveats" The local variable name can be the same as the member variable name, when used in the method, using the nearest principle.


5. Anonymous objects

Anonymous object: An object without a name, is a simplified representation of an object.

Two use cases for anonymous objects: 1. Object Invocation method only one time 2. Pass as actual parameter






This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1856056

Java-like and objects

Related Article

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.