What is "class and object" in java (brief description)

What is "class and object" in java

1. A brief description of the concept
Java code is based on classes, which contain the properties and behaviors of some specific things (objects).

The object is the concrete of a relatively abstract class. I even regard it as a small subset of the class. When using it, you create ("new") an object under the class you need to use to call the properties or behaviors inside. .

2. Application steps
First, make it clear that I created two classes:
①StudentAction (used to store the attributes and rules of student behavior)
②Manager (used to execute calls)

tep1在①中创建属性和行为

public class StudentAction {
    
    
 private String name;//学生属性
 private int score=0;
 
 //获取学生名行为
 public void SetName(String n) {
    
    
  name=n;
 }
 //学生学习行为
 public void study() {
    
    
  
  score++;
  System.out.println(name+"同学正在学习,学分+1");
}  
  //学生游戏行为
 public void play() {
    
    
  score--;
  System.out.println(name+"同学正在玩耍,学分-1");
}
//显示学分行为
 public void showscore() {
    
    
  System.out.println("目前学分是"+score);
  if(score>=5) {
    
    
   System.out.println("学分已修够!");
  }else {
    
    
   System.out.println("学分未修够!");
  }
 }
 }
在②中创建对象执行①中的行为

public class Manager {
    
    
public static void main(String args[]) {
    
    
  
  StudentAction st1=new StudentAction();
  st1.SetName("巴卡巴卡");
  for(int i=0;i<6;i++) {
    
    
   st1.study();
  }
  st1.play();
  st1.showscore();
 }
}

operation resultInsert picture description here

Guess you like

Origin blog.csdn.net/Lamont_/article/details/109079789