Java Object orientation

Concept: Object-oriented is based on procedural programming languages

Process-oriented: Emphasize each functional step

Object orientation: The emphasis is on the object, and then the object calls the function

Characteristics of object-oriented thinking:

1. It is an idea that is more in line with our habits of thought

2. You can simplify complex things

3: We programmed the leader from the executor

Classes and objects

How is something described in the real world?

For example:

Student: Student ID, name, gender, major, class, telephone number, address

Monitor, student committee,

We learn programming languages to simulate things in the real world, and the most basic unit of programming languages we learn is classes

Therefore, we should reflect things through the form of classes, and from this, we can get the correspondence between things and classes in the real world.

A class is a template or drawing that we use to design something

Object: is to build up the template or drawings we have designed.

Class: The keyword is class. A class is a template for creating objects. The properties and methods in a class are called class members.

Object: an object generated by an instantiation of a class. The keyword is new.

Class name variable name = new class name ();

Classes are statically created, objects are dynamically instantiated, programs operate on objects instead of classes, classes are modeled instead of objects, and objects are instantiated by classes

Class design based on Honor of Kings (run in Eclipse)

packageJava Basics 06_ Object-oriented;/* * Attributes: * Name, Damage, Movement Speed, Armor, Attack speed, Spell Strength, Health blue, Skill name */
public class Hero {
	String name;
	int gongjili;
	String jineng1_name;
	String jineng2_name;
	String dazhao_name;
	
	void jineng1(a) {
		System.out.println(jineng1_name);
	}
	void jineng2(a) {
		System.out.println(jineng2_name);
	}
	void dazhao(a) { System.out.println(dazhao_name); }}Copy the code
packageJava Basics 06_ Object-oriented;/* * Classes with main methods are called test classes */
public class HeroMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hero houyi =new Hero();
		houyi.name="Hou yi";
		houyi.gongjili=11;
		houyi.jineng1_name="Skills 1";
		houyi.jineng2_name="Skills 2";
		houyi.dazhao_name="1";
		
       houyi.jineng1();1 / / skills
       houyi.jineng2();2 / / skills
       houyi.dazhao();/ / big moves}}Copy the code

Example 2:

packageJava Basics 06_ Object-oriented;public class ShouQiang {
	String name;
	double zhongliang;
	boolean flag;
	int danliang;
	void kaiqiang(a) {/ / shoot
		System.out.println("Kill a man with a single shot.");
	}
	void huandan(a) {/ / in play
		System.out.println("Filling a bullet.");
		danliang=12;
	}
	void miaozhun(a) {
		if(flag) {
			System.out.println("Aim and aim");
		}else {
			System.out.println(name+"Target"); }}}Copy the code
packageJava Basics 06_ Object-oriented;public class ShouQiangMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShouQiang sq = new ShouQiang();
		sq.name="沙鹰";
		sq.zhongliang=6.44;
		sq.flag=true;
		sq.danliang=12;
		
		System.out.println("Famous gun:"+sq.name);
		for(int i=0; i<=sq.danliang; i++) { System.out.println("Opened its first"+i+"Gun");
			if(i==sq.danliang&&sq.flag==true) {
				 System.out.println(sq.name+"Aim and aim"); 
				System.out.println("Insufficient ammo, please fill in time!");
				System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
				sq.huandan();
				System.out.println("Ammo has been filled :"+sq.danliang); }}}}Copy the code