Statement of Java classes and object creation

  Class is a template that describes the state and behavior of a class of objects; the object is a specific instance of the class, instance of the class created by the process known as object class constructor. Such as: street light is a class, each class of objects is a street lamp.

1. Class declaration

  Class declaration format

[Public] [abstract | final] class indicates that the class // class name is followed by a statement of class, final represents the end of the class as a class can not be inherited; abstract to abstract class
[Extends superclass name] // represents the class inherits the parent class
[Implements the interface name list] {// class indicates that the interface to be implemented
       Data member declaration and initialization;
       Method declaration and method body;     
}

Example: Example WATCH

public class Clock{
	// member variables
	int hour;
	int minute;
	int second;
	
	// methods Member
	public void setTime(int newH,int newM,int newS){
		hour = newH;
		minute news;
		second = news;
	}
	
	public void showTime(){
		System.out.println(hour + ":" + minute + ":" + second);
	}
}

 

2. Object Creation

  Object names must be declared and object type prior to use, and then create an object using the keyword new.

  Statement object syntax: class name references the variable name

  Such as: Clock is the class name it has been declared now to declare a reference variable alock, used to store a reference to the class object:

Clock aclock; // this time did not generate an object, but a null reference

  Creating object syntax: new <class name> ()

aclock = new Clock (); // Clock type of object to allocate memory space in memory, the returned object reference
Reference object can be assigned a null value
aclock = null;

3. Data member

   Data member indicates the state of the object may be any type of data.

  1. declare data members  

[public|protected|private][static][final][transient][volatile]
Data type variable name 1 [= variable initial value], variable name 2 [= variable initial value], ...;

// public, protected, private operators for the access control
// static indicating that this is a static member variables (class variables)
// final value of the specified variable can not be modified
// transient specified variable is not required serialized
// volatile variable is a shared variable indicates

  2. instance variables - attributes describe the object

  No static modification of variables (data members) called instance variables;

 

  Store all instances required attributes, attribute values ​​may be different in different instances.

 

  Examples of access attribute values: <instance name> <instance variable name>.

// class round Circle.java saved in a file, the file ShapeTester.java test classes are held in two files
// placed in the same directory

public class Circle{
	int radius;
}

public class ShapeTester{
	public static void main(String[] args){
		Circle x;
		x = new Circle();
		System.out.println(x);
		System.out.println("radius = " + x.radius);
	}
}
//operation result
Circle@26b249
radius = 0

  3. class variables

    A static modification, only one value in the whole class, while it is assigned a class initialization for the classes in all objects of the same attributes, or frequent need to share data, or the need to use the system in some constant value.

  Citation format: <class name | instance name> <class variable name>

public class Circle{
	static double PI = 3.1415926;
	int radius;
}
// When we create an instance of the Circle class, and in each instance there is no stored value of PI, PI values ​​stored in the class

public class ClassVariableTester{
	public static void main(String[] args){
		Circle x = new Circle();
		System.out.println(x.PI);
		System.out.println(Circle.PI);
		Circle.PI = 3.14;
		System.out.println(x.PI);
		System.out.println(Circle.PI);
	}
}

//operation result
3.14159265
3.14159265
3.14
3.14

 

4. The method members

  1. grammatical form

[public|protected|private]
[static][final][abstract][native][synchronized]
Method Name Return Type ([parameter list]) [throw exceptionList]
{
	The method body
}
// public, protected, private access control
// static indicating that this is a static method
// final indicating that this is a termination method
// abstract indicating that this is an abstract method
// native code and to integrate java code in other languages
// synchronized multiple concurrent threads used to control access to shared data

// Return Type: java can be any data type, when the return value is not required, a void return type

// Parameter Type: it may be a simple data type, or a reference type (array, class or interface);
// number of parameters: there may be a plurality of parameters, the parameters can not, the method declaration parameter called the formal parameter

// method body: for the implementation of the method, including the declaration of local variables and all legal java statements, internal scoped method

// throw exceptionList: throw an exception list

  2. Examples of methods: represents a particular object's behavior, the former statement does not need to add static modification

   Call: a message to the object, by calling the object a method, using the object of an action / function.

   format:   

<Object name>. <Method name> ([parameter list])
<Object name> is the message recipient

 Parameter passing

  1. The value is passed: the parameter data type is a base type, argument passed by the parameter

  2. passed by reference: parameter type is a reference type

public class Circle{
	static double PI = 3.14159265;
	int radius;
	public double circumference(){
		return 2*PI*radius;
	}
	public void enlarge(int factor){
		radius = radius * factor;
	}
	public boolean fitsInside (rectangle r) {
		return (2*radius < r.width)&&(2*radius<r.height);
	}
}

public class InsideTester{
    public static void main(String[] args){
        Circle c1 = new Circle();
        c1.radius = 8;
        Circle c2 = new Circle();
        c2.radius = 15;
        Rectangle r = new Rectangle();
        r.width = 20;
        r.height = 30;
        System.out.println("Circle 1 fits inside Rectangle :" + c1.fitsInside(r));
        System.out.println("Circle 2 fits inside Rectangle :" + c2.fitsInside(r));
    }  
}

//operation result
Circle 1 fits inside Rectangle:true
Circle 2 fits inside Rectangle:false

 

  3. Class Methods: declare the need to add static modification

    Class methods can not be declared as abstract, can be invoked by class name, it can also directly call through the class instance.  

 

// converts Celsius (centigrade) to Fahrenheit (Fahrenheit)

public class Converter{
	public static int centigradeToFahenheit(int cent){
		return (cent*9/5 + 32);
	}
}

// method call
Converter.centigradeToFahrenheit(40)

  When the parameters are passed to variable length parameter, available ellipsis, and its essence is an array, such as a String ... s, actual parameters passed to the variable length parameter may be zero or more objects.

static double maxArea(Circle c,Rectangle... varRec){
	Rectangle [] rec = varRec;
	for(Rectangle r:rec){
		//...
	}
}

public static void main(String[] args){
	Circle c = new Circle();
	Rectangle r1 = new Rectangle();
	Rectangle r2 = new Rectangle();
	System.out.println("max area of c, r1 and r2 is" + maxArea(c,r1,r2));
	System.out.println("max area c and r1 is " + maxArea(c,r1));
	System.out.println("max area c and r2 is " + maxArea(c,r2));
	System.out.println("max area of only c is " + maxArea(c));
}

  

  

 

Guess you like

Origin www.cnblogs.com/thwyc/p/12238972.html