(Super detail) Java object-oriented programming - classes, objects, packages, parameters, constructor, overloading

A, classes and objects

1, Object

  • Characterized in - that each property attribute of each object has a particular value
  • Behavior - that method

2, type - Type Type class- class
from the abstract object "class", an abstract class is formed of a plurality of objects. It is the object model, is an abstract concept, but the object is a specific entity. Class is the mold features (attributes) and determines the object will have a behavior (methods).

3, the relationship between classes and objects

  • Class is a type of object
  • The presence of an object is a concrete class.

4, the package Encapsulation
objects have attributes and methods while both information, this feature is called encapsulation
properties and methods is typically packaged together to reflect the characteristics of things, both complement each other, they can not be separated.
Relationship with the object class :
class is a description of a class of things, abstract, concept definition;
and each individual subject is actually present such things, and thus also known instance (instance).
Process generated object , also known as instantiated (the instantiate) an object

Second, create classes and use objects

Object-oriented programming OOP (Object Oriented Programming)
1. Create a class
using the class keyword to define the | statement CLASS .-> Modeling

  • All Java programs to class class as an organizational unit
/**
*人 类
*/
public class Person {
	String name; //姓名.
	int age;
	//年龄
	char sex;
	//性别
	//方法的定义,声明
	public void eat() {
		System. out. println (name+"正在吃...");
	}
}

To object-oriented programming :
the real-world objects → (from concrete to abstract) → Modeling (creating classes) in Java programs in the world → program in the world using the class
to create objects → (from the abstract to the concrete) is directed to the object related programming operation.
create a class naming convention :
1, can not use the keyword Java
2, the first letter can be a letter, it can be _or $(not recommended)
3, can not contain spaces or .numbers
➢ class name first letter must be capitalized. After that word capitalized MyClass properties: myName, myScore
➢ class name commonly used nouns (see name to know justice)

public class Test {
	public static void main(String[] args) {
		int num; 1/分配了4字节
		num = 100;
		person p; //1.声明对象 - 分配了4字节
		p = new person(); //2.为p对象的属性分配空间
		//3.为对象的属性赋值
		p.name=张三";
		p.age=20;
		p.sex='男';
		//4.访问对象的属性或对象的方法使用圆点运算符p.属性或p.方法();
		p.eat(); //ca11调用方法
		Person p2 = new Person() ;
		p2.name="李四";
		p2.eat() ;
	}
}

Third, the special toString () method

After the class which defines the method, the object can be expressed as a string.
This method is defined in the class can be invoked implicitly.

//person类
public class Person {
	String name; //姓名.
	int age;
	//年龄
	char sex;
	//性别
	//方法的定义,声明
	public void eat() {
		System. out. println (name+"正在吃... ");
	}
	//tostring()用于将对象以字符串的形式来表达
	public String toString() {
		return "大家好,我叫"+name+",今年"+age+"岁,性别是"+sex;
	}
}
//测试类
public class Test {
	public static void main(String[] args) {
	Person P; //1.声明对象 .--分配了 4字节
	P = new Person(); //2.为加对象的属性分配空间
	//3.为对象的属性赋值
	p.name="张三";
	p.age=20;
	p.sex='男' ;
	//4.访问对象的属性或对象的方法使用圆点运算符p.属性或p.方法();
	System.out.println(p. toString()) ;
	//输出p对象时,则表示将p对象以字符串的形式来表达,此时会隐式调用Person类中的toString()方法
	System. out.println(p) ;
	}
}

Four, Java data type Summary

I wrote a detailed look at this article: the Java variables, identifiers, data types and their conversion
can be divided into two data types
1. Simple value type 8 - byte, short, int, long , float, double, char ., boolean
2. reference types - String, like int [] array type, etc., use the class keyword declared

Two types of differentiation
1) no value type variable properties and methods, can be assigned directly to
the referenced object types have attributes and methods, the attribute value can not be directly assigned
2) type of reference need to allocate memory (initialization) then via new longer property assignment
3) value type variable space allocated space in the stack, and a reference type object attribute assignment. hold a reference object inside the stack heap space in the heap space.

V. package package

Know the objects and classes, Java now look peculiar "package".
1. What is the package
package package program - the operating system is equivalent to the directory (folder Folder)
2. Why use a package
using package has three advantages:
1) the same name as the class name to avoid conflict.
2) allows the class organized into smaller units easy to find classes in java.util Import. Scanner, Arrays
3) using the package can protect class properties and methods
in the enterprise project development, specification decided that all classes must specify the package name, not the name of the package category does not exist
3. how to use package
1) definition of the package:
- creating packages directly
- create to create a class, while package
2) package naming conventions
- the names lowercase.
- domain name is used to name an inverted way -> cam. whos. javaoqp1. project1.group1. xxx
- customize Do not use a package name java.begins with java.util. *
3) the use of other types of packages must be imported import
- no package name of the class default package can not be imported
... -java lang * automatically import (program design using the Java programming language base class )
- If you need to use a sub-packet-based, must be introduced separately import
4) package -> import -> class order

package com.test;
import com. entity. *;
import com. entity. oop1. Person;
import com. entity. oop1.A;
public class Test{
	public static void main (String[] args) {
		Teacher t = new Teacher() ;
		System. out.println(t) ;
		Person p = new Person() ;
		System.out.println(p) ;
		A a= new A(); //A cannot be resolved to a type A类不能被解析
		System. out.print1n(a) ;
	}
}

Sixth, the scope of variables

Scope scope - acting range
according to the variable scope of the variables into the following two:
1. Local variables variable the Local
- Method Internal define variables
- the variables defined in the domain block
- shaped arguments involved in the method all belong to the various methods local variable
2. variable global variables, Ltd. Free Join
- property classes, member variables

Seven method calls with parameters

Method arguments can enhance the flexibility of the method.

方法定义部份参数→形式参数
方法调用部份参数→实际参数

And parameter arguments are local variables of each process.
reference method with parameter passing mode :
(1) for a call-by value Call
- type variable values, process modifications to the tone parameter value, does not affect the value of the argument
(2) by reference to the calling Reference Call
- reference object type
that references the primary caller argument method is called sub-object parameter held in modifying properties of an object in the parameter object, essentially modifying the argument attribute value objects.
In particular, the array as a method parameter
array is a reference type , as all parameters are " a reference call ."

package com.whos.demo04; .
class NumberTest{
	/**
	*查找最大值
	* @param arr
	*@return
	*/
	public int find_ large (int[] arr) { //形参
		int max = arr[0] ;
		for(int i=1 ; i<arr.length ; i++) {
			if (max < arr[i]) {
			max = arr[i] ;
			}
		}
		arr[0]=100;
		return max;
	}
}
pub1ic class Test {
	public static void main (String[] args) {
		NumberTest nt = new NumberTest() ;
		int[] nums = new int[]{32,45, 76,33, 11, 99,24};
		System.out.println ("调用前:"+nums[0]);
		int m = nt.find_ large (nums); //将数组名作为方法的实参
		System.out.println("最大值为: "+m);
		system.out.println ("调用后:"+nums[0]); // 值改变为100
	}
}

Eight, constructor

English name: constructor
, also known as the constructor , the constructor is a special method.

The conventional method in the definition there are two differences in form:
1> constructor does not write the return value of type
2> name of the same name as the class constructor [including the case]

In the conventional method invocation difference in:
1) an ordinary method call to an object by using the dot operator 对象名.方法名()
2 constructor method is called when the object is created), used new运算符to call the

There are two types of configuration
1> default constructor with no arguments [no parameters] - may not be written in a class, you may also call
2> constructor arguments
Example:

package com. whos. demo05;
class Person{
	String name; .
	int age;
	//Constructor
	//默认的无参构造器
	public Person() { .
		System. out. println("Person()默认构造器执行了... ");
	}
	public Person (String n,int a) {
		name = n;
		age = a;
		System. out. println ("Person (String, int)带参数构造器执行了...");
	}
	pub1ic void eat() {
		System. out. println (name+"正在eat() ...");
	}
}

pub1ic class Test {
	public static void main (String[] args) {
		Person p = new Person("小明",20) ;
		System. out. println (p .name+"\t"+p.age) ;
		p.eat() ;
	}
}

Note :
If the configuration parameters are defined in a class, in which case no want to use the default constructor parameters, the parameters must be no constructor is also defined in the class.

Overload Nine, methods

Overload - refers to a method of the same name appears in a class, but different parameters:
1> number of parameters
2> type parameter
satisfies any one of the reload is achieved.
! ! ! Method Return Value Type and overloading independent
advantages: a method to avoid the cumbersome name.

package com. whos. demo05;
class Person {
	String name;
	int age;
	/ /Constructor
	//默认的无参构造器
	pub1ic Person() {
		System. out. println ("Person ()默认构造器执行了... ");
	}
	public Person (String n,int a) {
		name = n;
		age = a;
		System. out. println ("Person (String, int)带参数构造器执行了...");
	}
	public void eat() {
	System. out. println (name+"正在eat() ...");
	}
	public String eat (int money) {
		return "";
	}
}

pub1ic class Test {
	public static void main (String[] args) {
		Person p = new Person() ;
		System. out. println (P .name+"\t"tp.age) ;
		p.eat() ;
	}
}
Published 53 original articles · won praise 60 · views 4924

Guess you like

Origin blog.csdn.net/qq_37717494/article/details/104918738