Class and Object (i)

Source: Internet
Author: User
one: The relationship between classes and objects A class is a description of an object, a "template" for creating an object, an abstraction of an object An object is a concrete thing in the real world, an instance of a class, an instantiated result of a class .
Class Name {      	
	attribute declaration;
	method declaration;
}


Defines a class
people{
	//class Property declaration (variable definition) int age
	;
	float height;
	The function declaration (method definition) of a class is
	void Printage () { 
		System.out.println (age);
	}
	void Updateage (int a) {
		age=a
	}  
}


the Declaration and creation declaration object and the declaration variable are similar, for example: people p;

Create object using keyword New

Class test{public
	static void Main (String [] args) {
		people p; 
		p=new people ();
		People her=new people ();
		People yp1=new people ();
	}


Use of Objects
Class test{public
	static void Main (String [] args) {
		people p; 
		p=new people ();
		p.age=20;
		P.printage ();
	}


Formal parameters and
arguments
the concept of formal parameters and arguments formal parameters: all called "formal arguments" are parameters that are used when defining method names and method bodies to receive the actual values passed in when the method is invoked arguments: All called "actual parameters", the actual values passed to the method when the method is invoked Attention the actual parameter type given when the method is invoked is the same as the form parameter type when the method is defined, and the order is the same method is called
Class people{
	void Updateage (int a) {
		System.out.println (a);
	}  
}


Class test{public
	static void Main (String [] args) {
    	people My=new people ();
		My.printage (); XXX
		my.printage ("John"); xxxx
		my.printage (20.12); xxxxx
		My.printage (a);


Recursive method
recursion is calling yourself in a method
For example, if a method is sort (), if there is a statement in this method that calls itself: the
	static void sort () {
		...
		). Sort ();
		...
	}
You can call this method a recursive method

-Construction Methods
A construction method is a special way to initialize the internal state of an object

Class test{public
	static void Main (String [] args) {
		people I; 
	     my=new people ();
		my.age=20;
		My.printage
	}
}
people (): When instantiated, it is equivalent to calling the default constructor method without parameters. the particularity of the construction method The main function of the construction method is to finish initializing the class object. When you create a new object of a class, the system automatically calls the constructor of that class to initialize the new object The constructor method cannot be called explicitly directly by the programmer construction method has no return type the method name of the constructor is the same as the class name Summary in the Java language, each class has at least one construction method if the definition of a class does not explicitly define any constructor methods, the Java compiler will automatically provide a default constructor for the class, with no parameters for the default constructor method, no method body for the default constructor in a Java class, once the definition of a class explicitly defines one or more constructed methods, the system will no longer provide the default construction method overloading of constructed methods
class Person {
	int age;
	Person () {age = 18;}//constructor person with no parameters
	(int i) {age = i;}///with constructor for parameter
	void setage (int i) {age = i;}
}


Class Test {public
	static void Main (string[] args) {person
		p1=new person ();
		Person P2=new person (n);
	}

Depending on the passing parameters, Java automatically chooses the corresponding construction method.


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.