This week it is mainly to learn construction methods, as well as Java object-oriented features encapsulation .
Example:
Topic : Define and test a class called Student, including the attributes "study", "name", as well as three courses "Mathematics", "English" and "computer" results, including methods have three courses the "Total", "average", "highest score" and "the lowest score."
Experiment Code
package text1;
class Student implements PersonUser {
private String stuno;
private String name;
private float math;
private float english;
private float computer;
public Student(String stuno, String name, float math, float english, float computer) {
this.stuno = stuno;
this.name = name;
this.math = math;
this.english = english;
this.computer = computer;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getStuno() {
return stuno;
}
@Override
public void setStuno(String stuno) {
this.stuno = stuno;
}
@Override
public float getMath() {
return math;
}
@Override
public void setMath(float math) {
this.math = math;
}
@Override
public float getEnglish() {
return english;
}
@Override
public void setEnglish(float english) {
this.english = english;
}
@Override
public float getComputer() {
return computer;
}
@Override
public void setComputer(float computer) {
this.computer = computer;
}
@Override
public float sum() {
return math+english+computer;
}
@Override
public float avg() {
return this.sum()/3;
}
@Override
public float max() {
float max=math;
max=max>computer?max:computer;
max=max>english?max:english;
return max;
}
@Override
public float min() {
float min=math;
min=min<computer?min:computer;
min=min<english?min:english;
return min;
}
}
package text1;
public class Ceishi {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student per1=null;
per1=new Student("1","张三",95.0f,89.0f,96.0f);
System.out.println("学生编号:"+per1.getStuno());
System.out.println("学生姓名:"+per1.getName());
System.out.println("数学成绩:"+per1.getMath());
System.out.println("英语成绩:"+per1.getEnglish());
System.out.println("计算机成绩:"+per1.getComputer());
System.out.println("总分:"+per1.sum());
System.out.println("平均分:"+per1.avg());
System.out.println("最高分:"+per1.max());
System.out.println("最低分:"+per1.min());
}
}
Test Results
Also learned about this class, static class
this
Attribute Access : Access this class of property, from the parent class if this class does not have to continue to find this property.
Methods : access methods in this class, this class from the parent class if this method does not continue searching.
Call the constructor : call this class structure, it must be placed on the first line of the constructor.
Special : represents the current object.
static
If you use the static properties declared in the program, then this property belongs to the global property; attribute static declaration is shared by all objects, the best can be called directly by the class name when accessing static properties.
static attribute may be used in the statement of the time, you can use it to declare a method, a method which is sometimes called the statement of class methods can be called directly by the class name.
Non-static method can be declared to call a property or method of static declaration. But the method is declared static properties or methods can not call non-static type declaration of
For the main () method has also been some learning, we know the meaning of the main () method for each parameter. Another way to learn to cycle through the array, as follows:
for(String e:args){
System.out.println(e);
}