Basic understanding of the use of classes and objects

concept:

Class: having the same characteristics (attributes) and functions, object behavior (method) is classified as a class.

Object: a specific instance of a class of things.

Format: create a class {} public class name of the class

   Public property type defines the class name attribute;

   Public class defines methods return type of the method name (parameter type parameter name, ....) {method thereof, the method performed} content parameters: methods require the use of external conditions.

Use: The method object name attribute object name body (parameter values, ......);.

Advantage of using object-oriented development: programmers thinking compliance, ease of development.

Difficulties: modeling the scene, extracting required classes, and class attributes and methods.

example:

  Scene: students in learning java course, learning to make every lesson courses decreased by 1, repetition, reduce the hours until the end of the course is zero, learning is completed, the student's credits increased credit java course.

  The first step: analysis of objects in the scene: Java courses students

  Step two: object properties and methods of analysis: Java courses attributes: class, credits, course name method: \

                  Students attributes: name, credit method: learning parameters: Attend Procedure: 1. Let course reduce class 1 2. 3. determined whether learning to get credit

  Step 3: Create class

  Step Four: Create an object in the main method, call the method by scene.

 

example:

 

Hero.java file

public class Hero {
private String name;
private int blood;
private int attackValue;

public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getAttackValue() {
return attackValue;
}
public void setAttackValue(int attackValue) {
this.attackValue = attackValue;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Hero() {

}

public Hero(String name,int blood,int attackValue) {
this.name = name;
= Blood this.blood;
this.attackValue = attackValue;
}

public void Attack (Hero H) {
this.blood = Blood - h.attackValue;
IF (Blood <= 0) = 0 Blood;
System.out.println (H. name + "attacked" + name + "," + name + " reduced" + h.attackValue + "blood," + name + "current health as" Blood +);
IF (Blood == 0) {
System.out.println (name + ". dead" + h.name + "victory, current health as" h.blood +);
return;
}
}
}

 

TestHero.java file

public class TestHero {

public static void main(String[] args) {
Hero h1 = new Hero("韩信",110,10);
Hero h2 = new Hero("盖伦",100,10);

while(h1.getBlood()>0 && h2.getBlood()>0) {
h1.attack(h2);
if(h1.getBlood()<=0) break;
h2.attack(h1);
}
}

}

 

Output:

Galen attacked Han, Han reduced blood 18, blood 92 current Han
Han attack Galen, Galen reduced blood 20, blood 80 Galen current
galenic attack Han, Han reduced 18 blood, blood is currently 74 Han
Han attack Galen, Galen reduced blood 20, blood 60 Galen current
galenic attack Han, Han reduced blood 18, blood 56 current Han
Han attacked Galen, Galen reduced blood 20, blood 40 Galen current
galenic attack Han, Han reduced blood 18, blood 38 current Han
Han attack Galen, Galen reduced 20 blood, Galen current health of 20
Galen attacked Han, Han Xin reduction of 18 blood, Han Xin current health of 20
Han attacked Galen, Galen reduces the amount of blood 20, Galen current health 0
Galen died. Han victory, current health of 20

 

Summary: The object-oriented, easy to develop, will be extracted objects in the scene, properties, methods. From process-oriented thinking transition to object-oriented thinking.

Guess you like

Origin www.cnblogs.com/pmz-blog/p/11009659.html