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