Today, I learned the basic concepts of classes and objects. I wrote a small program with java and my knowledge. Now I summarize the learning content.
I Basic concepts of class and object.
Class, that is, the abstraction of something. As far as I understand, it is to use computer code to write the category of something that needs to be abstracted. It can also be said to be the writing of the same type of data and the operation of data. For example, students have some common attributes, characteristics and actions, which are a kind of things. We can use the code to write it and abstract a student class.
Object, that is, a specific instance in a category.
A class can be said to be an abstraction of an object. An object is a concrete instance of a class. The object occupies the actual storage space, and the class does not occupy the storage space. The code does not contain an actual object.
II Class structure and object principle
Class structure:
1) Class, that is, the description of the object. Is a description that defines the specific data type of the object itself in the class. Nominality is expressed by a word / data. Format: attribute type attribute name.
2) Class, that is, the behavior of the object. A method is an independent applet with specific functions defined in a class. It is also called a function,. Format: return value type, method name (parameter list) {method body}, data type of the return result after the method is run. If there is no return value, void is used.
3) Main program entry (in Java, the main program is defined inside the class, while C + + is outside the class)
Object principle: objects are created by classes and occupy actual storage space. Various methods and operations defined in classes can be used. The object name is a pronoun when the program operates the object again. The object name can call the attribute assignment, and the object name can call the method function for execution.
III Create object procedure and object name call properties and methods
Keyword for creating object: new.
Format: class name object name = new class name ();
Call of object name attribute: object name attribute
Call of object name method: object name Method (method parameter list)
IV PK game code
package com.cs0112; import java.util.Random; public class Hero { String name; int id; int hp; int ad; /* Attack, in which the attack of one object on another object has a 10% probability of being avoided */ public void ack(Hero h) { if(h.name != name) { h.hp -= ad; System.out.println(name+"Attacked"+h.name); Random random = new Random(); int number = random.nextInt(10); if(number == 0) { h.hp += ad; System.out.println(h.name+"Dodged"+name+"Attack"); System.out.println(h.name+"HP left:"+h.hp); } else { System.out.println("send" + h.name + "Lost" + ad + "spot HP"); System.out.println(h.name + "HP left:" + h.hp); } } else { System.out.println("Heroes cannot attack themselves"); } } /*Recover, the hero will automatically recover 50 HP after each turn*/ public void heal() { if(hp>0) { hp += 50; System.out.println(name+"Your health remains after recovery:"+hp); } } //public void dodge(); public static void main(String[] args) { Hero lvbu = new Hero(); lvbu.name = "Lv Bu"; lvbu.hp = 2800; lvbu.ad = 180; Hero houyi = new Hero(); houyi.name = "Hou Yi"; houyi.hp = 2000; houyi.ad = 280; int count = 0; while(lvbu.hp > 0 && houyi.hp > 0) { count++; System.out.println("-----The first"+count+"Round!-----"); lvbu.ack(houyi); houyi.ack(lvbu); lvbu.heal(); houyi.heal(); } if(lvbu.hp>0 && houyi.hp <= 0) { System.out.println("-----End of turn-----"); System.out.println("Lv Bu wins the game!"); } else { System.out.println("-----End of turn-----"); System.out.println("Hou Yi won the game!"); } lvbu.ack(lvbu); } }
Operation results of main functions:
-----Round 9-----
Lv Bu attacked Hou Yi
Hou Yi lost 180 HP points
Hou Yi's HP remaining: 780
Hou Yi attacked Lv Bu
Lv Bu dodged Hou Yi's attack
Lv Bu's HP remaining: 960
Lv Bu's HP remaining after recovery: 1010
Hou Yi's HP remains after recovery: 830
-----Round 15-----
Lv Bu attacked Hou Yi
Hou Yi lost 180 HP points
Hou Yi's HP remaining: 360
Hou Yi attacked Lv Bu
It made Lubu lose 280 HP points
Lv Bu's HP remaining: - 140
Hou Yi's HP remains after recovery: 410
-----End of turn-----
Hou Yi won the game!
Heroes cannot attack themselves