Java object-oriented-classes and objects

Source: Internet
Author: User

Java object-oriented-classes and objects

Class-to-object relationships

We take a popular example, such as human is a class, Zhang San this person is a specific human individual, that is, the object in Java, this is a class and object relationship;

Definition of Class

See example below

Creation and use of classes

Look at the following example

We're building a new person class under the COM.JAVA1234.CHAP03.SEC01 package.

1  Packagecom.java1234.chap03.sec01;2  3 /**4 * The person class file name and class name must be consistent5  * @authorUser6  *7  */8  Public classPerson {9  TenString name;//in the class, define a name for the string property can exist string similar to "Zhang San" One     intAge//in the class, define an Age attribute A       -     /** - * Define a method public represents a common permission maximum void means that the return value is null speak is the method name in parentheses can be added parameter the      */ -      Public voidspeak () { -SYSTEM.OUT.PRINTLN ("I call" +name+ "I am this year" +Age ); -     } +       -      Public Static voidMain (string[] args) { +         //defines an object for a person class Zhangsan A Person Zhangsan; at         //instantiating an object -zhangsan=NewPerson (); -         //Assigning a value to the object's Name property -Zhangsan.name= "Zhang San"; -         //Assigning a value to an object's age property -Zhangsan.age=23; inZhangsan.speak ();//methods for calling objects -     } to}

Memory analysis

Java object-oriented-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.