Java object-oriented knowledge

(1) The origin of object-oriented:
1. The software crisis in the mid-60s-the backward software production method cannot meet the rapidly increasing demand for computer software, which leads to a series of serious problems in the process of software development and maintenance .
The standardization and reusability of house arrest have been highly recognized by the industry. It has played an important role in avoiding duplication of labor and alleviating the software crisis.
2. Everything is
an object. Everything can be regarded as an object. Insert picture description here
3. Use object-oriented thinking to describe the display of the world. The
basic steps
(1) find the class
(2) find the attribute (noun)
(3) find the
data abstraction of the behavior (verb) .
Definition class

//演员类
public calss Actor{
    
    
//共同特性
public String name;
public char sex;
public String job;
public int age;


//共有属性

public void eat(){
    
    ........}
public void performs(){
    
    ..........}

}

4. Describe classes with class diagrams-UML
This picture above
+ stands for public
-stands for private
reference tools: StarUML, Astah (cracked version available).

UML diagram generated by Astah
The export of Astah tool will automatically generate classes.
Summary:
The advantages of object-oriented: a new way of organizing code, born for large programs, small programs experience no object-oriented benefits.
The principle of object-oriented abstraction When
designing a class, don't let the class face the concrete class, but face the abstract class or interface.
(2) Example demonstration
Requirements description
1. Define the class and create the object
2. Print the information of the object
Insert picture description here

//第一个类
//创建劳拉和孙悟空的类
//创建游戏角色类Role
public calss Role{
    
    
	/*名称属性*/
	public String name;
	//等级
	public int level;
	//职业
	public String job;
	//释放技能
	public void  sendskill;
public void doSkill(){
    
    
	if(name.equals("劳拉")){
    
    
	System.out.println("劳拉释放了她的必杀技————隐身夺萃刃");
	
	}else if(name.euqals("孙悟空")){
    
    
	System.out.println("孙悟空释放了他的技能————吃俺老孙一棒")
		}
	}
}
//第二个类RoleTest
//实例化
 //声明一个Role类型的变量,Role role1相当于在栈空间中申请了一块空间role1,new Role();相当于在堆空间里分配了一块空间;
 Role role1=new Role();//把申请的地址赋给了role1;
 role1.name="劳拉";
 role1.level=25;
 role.job="考古学家";
 //调用方法释放劳拉的技能
 role.doSkll();
 
//System.out.println(role1)可以打印出哈希码的值
 Role role2;
 role2=new Role();
 role2.name="孙悟空";
 role2.level=99;
 role2.job="齐天大圣";
 //调用方法释放孙悟空的技能
 role2.doSkill();

(3) The use of the
constructor The characteristics of the constructor:
1. No return value
2. The method name is consistent with the class name
Example 1

public Role(String name1,int level1,String job1){
    
    
name=name1;
level=level1;
job=job1;
}

//调用
Role role1=new Role("孙悟空","99","齐天大圣"){
    
    
 role1.show();
}

Example two

public class Circle{
    
    
	//圆的半径
	public double radious;
	//圆的周长
	public double perimeter;
	//圆的面积	
	public double area;

	//默认构造
	public Circle(){
    
    
	inputRadious();
	}
	
	//带参构造
	public Circlev2(double radious){
    
    
	if(radious){
    
    
		radious=radious();
	}else{
    
    
	inputradious();
	}
	}
	public void inputradious(){
    
    
	Scanner input=new Scanner(System.in);
	System.out.println("请输入半径");
	radious=input.next.Double();
	input.close();
	}
	public void showPerimeter(){
    
    
	if(radious==0){
    
    
	inputRaius();//如果用户没输入半径,强制用户输入半径
	}
	perimeter=2*Math.PI*radious;
	System.out.println("周长为"+perimeter);
	}
	public void shouArea(){
    
    
	area=radious*radious*Math.PI;
	System.out.println("面积为"+area);
	}
}


//测试类

//(1)构造方法生成
Circlev2 circle=new Circlev2();
circle.showArea();


//(2)重载
public class CircleTest{
    
    
	public static void main(String[] args){
    
    
	Circle circle=new Circle();
	circle.inputRadious();
	circle.shouPerimeter();
	circle.shouArea();
	}
	}

Guess you like

Origin blog.csdn.net/qq_44143902/article/details/109076382