Object-Oriented basic 01 (class instance analysis) Implementation objectives
1. How to analyze a class (Basic analytic thinking of Class)
The idea of analysis
1. Write down the properties contained in the class as required
2. All attributes must be encapsulated (private)
3. Properties after encapsulation are set and acquired through setter and getter
4. If necessary, a number of construction methods can be added
5. Add the appropriate method according to other requirements
6. All methods in the class are not output directly, but are handed to the invoked
Demo
Define and test a class named student, including attributes with "number", "Name" and 3 courses "math", "English", "computer" scores, including methods to calculate 3 courses "Total score", "Average score", "Highest score" and "Minimum score".
1. Properties and types of this class
2. Define the required methods (construction method, common method)
3. Class Diagram
4. Writing code
/***1. Define a Student class and test the class */class student{private String stuid; Study number private String name; Name private float math; Math results private float 中文版; English results private float computer; Computer score Public Student () {}//define 5 parameters for the constructor, initialize public Student for the properties in the class (String stuid,string name,float math,float E Nglish,float computer) {this.stuid = Stuid; THIS.name = name; This.math = Math; This.english = 中文版; This.computer = computer; } public void Setstuid (String stuid) {this.stuid = Stuid; } public String Getstuid () {return this.stuid; } public void SetName (String name) {this.name = name; } public String GetName () {return this.name; } public void Setmath (float math) {this.math = math; } public float Getmath () {return this.math; } public void Setenglish (float 中文版) {this.english = 中文版; } public float Getenglish () { return this.english; } public void Setcomputer (float computer) {this.computer = computer; } public float Getcomputer () {return this.computer; } public float sum () {Float sum = this.math + this.english + this.computer; return sum; } public float avg () {float avg = (This.math + this.english + this.computer)/3; float avg = this.sum ()/3; return avg; } public float Max () {float max = This.math; max = this.english > Max? This.english:max; max = this.computer > Max? This.computer:max; return Max; public float min () {float min = This.math; min = this.english < min? This.english:min; min = this.computer < min? This.computer:min; return min; }/***1. Write the test class, test the above code */class demo01{public static void Main (String [] args) {Student stu1 = new Student ("1114 020116 "," Zhang San ", 95.0f,78.0f,85.0f); Instantiate a student object and assign a value of Sys by constructing the methodTem.out.println ("Study No.:" +stu1.getstuid ()); System.out.println ("Name:" +stu1.getname ()); System.out.println ("Math Score:" +stu1.getmath ()); System.out.println ("English Score:" +stu1.getenglish ()); SYSTEM.OUT.PRINTLN ("Computer Score:" +stu1.getcomputer ()); SYSTEM.OUT.PRINTLN ("Total Score:" +stu1.sum ()); System.out.println ("Average of results:" +stu1.avg ()); System.out.println ("Highest score:" +stu1.max ()); System.out.println ("Lowest score:" +stu1.min ()); }}process started >>> No.: 1114020116 Name: Zhang San Math score: 95.0 English score: 78.0 computer score: 85.0 Total: 258.0 results average: 86.0 High: 95.0 min.: 78.0 <<< Process finished. (Exit code 0)
Java Basic Learning 05 (object-oriented base 01--class instance analysis)