Learning java --- object-oriented (on)

Knowledge Review:

1) abnormality (abnormal business users strange, there are many wonderful, no matter how the code is written, he indiscriminate importation, illegal for these special values, the program should be able to avoid normal.)
A) try, possible error code in the try , try not to put the code can not be inside
b) catch, catch the exception, if you try the code wrong, it will jump directly to the catch in the execution process known in advance may be wrong, exception handling, lifting: "procedural problems, please contact administrator "
c) Exception, get exception information: e.getMessage () error description.
d) throw, throwing an exception, they can not deal with, is thrown to the superior (calling category)
E) throws, reception anomaly, it must call the class received these exceptions, their processing either try / catch, or thrown, it calls class must be addressed. The eclipse has prompted.
2) Process control
a) execution order. From top to bottom, left to right. Note the assignment statement executes = the right, the right to assign the result to the left.
b) branch execution
i single branch:. if () {} If the condition is true true, the execution code inside, or not performed, IF () {} the else {}
II multibranched:. if () {} else if ( ) ... else {}, switch ( phone) case 110, break, switch statement can be used elseif alternatively
c) loop is executed
i for, for (int i = 0;. i <10; i ++) {}, foreach later development will learning to object variable
II. while, can be transformed into for, while more flexible, while in the previous initialization variables noted, while in vivo, it is necessary to change the decision value. i ++, I do not do it the cycle of death.
iii. do-while, essentially while, the difference between them. While the cycle time may not perform, do-while because the judge is in the implementation of the code behind, so both conditions are satisfied, at least once. (Read the disk file)

Today contents:

1) object-oriented
2) Java is the most important thing: classes, objects (java things are objects)
3) analysis of class structure: class, member variables, constructors three elements
4) method overload

1) Object-oriented, program code before, the design (outline design, detailed design, the design class), there are different ways of thinking
a) for the process, the traditional way of thinking is a process-oriented, do one thing, the first, second, Third, focus on the completion of the entire business process.
b) object-oriented, new ways of thinking, which objects involved in business, what object has properties and behavior, focusing on business entities (objects)

Example: a classic case: the elephant into the refrigerator several steps?
. a process-oriented analysis and our daily way of thinking :( Like)
First, find an elephant
Second, the refrigerator door open
third, the elephant into the
fourth, the refrigerator door closed
b Object Oriented Analysis:
First this process involves looking for business objects :( noun) attention is much more extensive, more than previously thought, even if some being less than
the elephant (property: old, body weight, limbs, tail, eyes ...; behavior (actions ): running, walking, eating ...)
refrigerator (attributes: size, a few open, frozen to a few ...; acts: electricity, refrigeration function)
object-oriented way of thinking than process-oriented way of thinking a lot more thinking, abstract more difficult.

Process-oriented, c statement, vb, 2004, contact with java pure object-oriented language.

Why mainstream programming design ideas: object-oriented
development can not be avoided, there is a problem, the problem will result in project delays or even unable to deliver, even software yellow.
The demand for change: to finish the user is not satisfied, you do not say what he wanted?

a. began to demand, the elephant into the refrigerator on it
b. the middle and make a demand, elephant how much? (Procedure for revising the code)
c. Also proposed a requirement, must Haier refrigerator (procedure for revising the code)
to consider a lot before object-oriented, hidden needs of users are covered in our design, just modify the code a little bit, At the same time, it will not affect the overall design.
Conclusion: Now developers are "object-oriented" as the core development approach. In fact, the actual development in many places are "process-oriented." It is the test software designers design capability. General business senior, senior system analyst to do (8 years +, many years of experience), the requirements for beginners, these names, you can understand the concept.

2) Object-oriented OOP, Object Oriented Programming Object-oriented programming language,
a) encapsulation
b) inheritance
c) polymorphism
d) Abstract

3) Java objects that everything in
a) class
b) objects

This method must return a result of type Integer
This method must have a return value, the return value of type Integer
Can Not the make A static Reference to The non-static Method ADD2 (Integer, Integer) from The type the HelloWorld
the Main is a static method, it can not be non-static method call add2

package cn.tedu;
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World");
		//静态方法调用	public static Integer add()
		Integer r1 = HelloWorld.add1(10, 20);
		System.out.println(r1);		
		//普通方法调用
		//HelloWorld.add2(10,20);
		//对应非静态方法,必须先创建对象,然后再调用
		//helloWorld就称为对象
		HelloWorld helloWorld = new HelloWorld();
		Integer r2 = helloWorld.add2(10,20);
		System.out.println(r2);
	}
		//创建一个方法,整数相加方法 z=x+y
	//java中尽量用包装类型
	//有两个参数
	public static Integer add1(Integer x, Integer y) {
		Integer z = x + y;
		return z;
	}	
	public Integer add2(Integer x, Integer y) {
		Integer z = x + y;
		return z;
	}
}

Rules: Do not use static methods, it is more resource accounting

Class: a blueprint for dead, you can not call a class method (add) directly, only one (to build a house drawing)
Object: examples of actual cases, the specific content, live, you can call the object methods (add), a class You can create multiple objects (concrete floor, the same drawing can cover several buildings)

The HelloWorld = new helloWorld the HelloWorld ();
the HelloWorld the HelloWorld h1 = new ();
the most common way to create a new object by new keywords,

4) the class consisting of three elements
a) construction methods, when used to create objects, set the initial value
b) member variables (attributes)
common method c) (function)
human:
constructor: class method if you do not write configuration, java compiler optimization, it will automatically create a default constructor. Because the class must have a constructor to create a class, you can use the new keyword.
Member variables: name, age
common methods: work ()

package cn.tedu;
public class People {
	//私有成员变量
	private String name;		//姓名
	private Integer age;		//年龄
	//普通方法
	public void work() {
		System.out.println("我去工作了");
	}
}

Remember: The default value type of packaging is null,

有参构造方法比默认构造方法更加灵活,用户可以传入动态值。
package cn.tedu;

//私有成员变量,普通方法,默认构造方法,带参构造方法
public class People {
	private String name;
	private Integer age;	
	public void work() {
		System.out.println(name+"开始工作了,他的年龄是:"+age);
	}	
	//默认构造方法:没有返回值,方法名字和类名一致,一个类中只能有一个默认构造方法
	public People() {
		name = "tony";
		age = 18;
	}		
	//带参构造
	public People(String name, Integer age) {
		//this就代表这个类
		//成员变量和参数同名,此时成员变量可以通过this来明确区分
		this.name = name;
		this.age = age;
	}
	}
package cn.tedu;
public class TestPeople {
		public static void main(String[] args) {	
		//调用默认构造方法
		People p1 = new People();
		p1.work();		
		//调用带参构造方法
		People p2 = new People("任正非",60);
		p2.work();		
		People p3 = new People("James Pond", 70);
		p3.work();	
		}}

Morning Summary:

1) the object-oriented and process-oriented Reflection different ways
a) for the process, the process is concerned, the first, second, third. . .
b) object-oriented, concerned about is the entity (noun statement, elephants, fridge), member variables (size, weight, color ...) and methods (walking, running, Sahua ...)
c) not easy to understand, it takes a long time. Slowly
2) classes and objects HW = new new the HelloWorld the HelloWorld ();
A) Class: HelloWorld, dead, can not directly access member variables and methods, Blueprint (drawing a house)
B) Object: hw (example), live , access to member variables and methods, specific real thing (more specifically house)
general objects are new out.
3) class consists of three parts
a) a private member variable, to make use of package type
I Private String name.
II Private Integer Age.
B) an ordinary method
c) constructor:
I no parameters default constructor People p = new People (); .
. ii configured with a parameter (more flexible) People p = new People ( "Ren", 60);
default constructor, no return value, the class name and the method name to be consistent, a class can have a default constructor
that they can do not write the default constructor, if no other constructor, the default constructor will be automatically added when java compiler optimizations.

problem:

1) construction method requires only a fall, when writing code for new People () This is the call the default constructor, if you did not write the constructor, java at compile time, when generating .class file will be automatically added to the default constructor. Arg constructor specify when calling the band, the default constructor is not required to perform.
2) create a test class method must call it? You can not, you can usually write the main method in their own classes, you can create a separate test class for testing. The latter is a good habit.
3) static method and the difference between ordinary method. The regular course also talk in depth
a) static method: HelloWorld.add ();
b) common methods: the HelloWorld hw = new new the HelloWorld (); hw.add ();
4) the difference between Integer and int
a) in a method body use basic variables, basic types no additional method, a small share memory space. The advantages of small footprint, run out of resources to be released. Shortcomings, there is no type of packaging these conversion methods, a lot needs to write. Development of trouble.
b) Try packing a member variable of a class type, also known as reference types, object that there are many additional method hashCode (), intValue (), toString (). . . With these methods will take up memory. Convenience advantages, development, java is called object-oriented programming, it is in the content of the class, the class is instantiated object. Package Type themselves objects, and these objects can be seamlessly. But the basic type must be converted. Development is very convenient, you can directly call the corresponding method. Cons: total memory.

to sum up:

1) object-oriented and process-oriented: a completely different way of thinking, the actual development of a combination of both
a) process-oriented, focus: process, what to do first, what to the second step. The first step to find the elephants, the second step, the refrigerator door is opened, the third step, the elephant in the refrigerator (the thought process)
b) object-oriented, focus: physical (elephant (attributes: name, size tail ... the behavior: method, running, jumping, playing ...), refrigerator, general language to describe nouns)
2) classes and objects
a) class is the blueprint, drawing, dead, you can not directly access the internal what the HelloWorld
B) the object, create an instance of the class, live, can access the class attributes and behavior HelloWord hw = new HelloWorld (); , hw is called an object instance
3) from the class consisting of three parts
a) construction method, from the initial value role. private String name; where its value is not set, the value may be set by the constructor.
b) member variables (attributes), data transmission, data encapsulation. Down the data temporarily stored, you can access elsewhere
c) an ordinary method (behavior), processing data, add (x, y) = x + y. algorithm.
4) constructor
a) default constructor, create an object must have a default constructor. If you do not write a default constructor, and no other constructor will automatically add the default constructor java compile time. If there are other construction methods, even with ginseng is not created.
b) the parameter configuration method, a plurality of parameters, see the traffic situation People (String name, Integer age, String address ...) when setting up, the user can have a constructor parameter, the parameter value passed in. Equivalent to the default settings.
c) provide: no return value; must be the same method name and the name of the class. Can have parameters, no parameters, can not be repeated. The default constructor can be only one. When you create a class, only a constructor can create. You do not need to call other constructors.
5) members and local variables
a) is a slave type member variable, this class member variables can be directly used
b) is a local variable dependent method, the local variable in the method can only be effective, it does not take out of
these, called variable declaration period. Member variable length class, an instance of the object class die, it is not available. Local variables method ends to perish, short

6) POJO simple or pure java object
a) provides that: only 2 content. A bunch of private property, a bunch of get / set methods
b) toString not used to the user, so it exists or not does not matter. It is a call to developers. Test values are correct.
. 7) the User POJOs
Public class {the User
Private ID Integer;
Private String name;
Private Integer Age;

  //利用eclipse生成所有get和set方法,右键source菜单,generate生成		 get/set方法
  //利用elipse生成toString方法

}

Published 36 original articles · won praise 13 · views 1080

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104703575