練習一:
package sonyi;
/*
練習:
定義一個表示學生的類。屬性有:學号,姓名,年齡,計算機成績,數學成績,英語成績。
要求學号為類變量,每次增加學生時,學号自動增加,并指派給新增學生。計算學生的平均成績和總成績。
執行個體化幾個對象,并測試。
*/
public class StudentTest {
public static void main(String[] args) {
//建立學生類,學号自動增加,其他參數由構造方法傳入
Student s1 = new Student("zhangsan",20,95,80,78);
Student s2 = new Student("lisi",20,95,80,78);
System.out.println(s1);//列印對象就是列印對象的toString方法。
System.out.println(s2);
//設定學生學号從10開始
Student.index = 10;
Student s3 = new Student("lisi",27,95,80,78);
System.out.println(s3);
Student s4 = new Student("lisi",27,95,80,78);
System.out.println(s4);
}
}
class Student{
static int index = 1;//定義一個靜态變量,用來記錄目前新增學生的學号
//定義私有成員變量
private String name;
private int age;
private int num;
private double computerScore;
private double mathScore;
private double englisdScore;
Student(){
}
//重載構造方法,傳入實參
Student(String name,int age,double computerScore,double mathScore,double englisdScore){
this.name= name;
this.age = age;
this.computerScore = computerScore;
this.mathScore = mathScore;
this.englisdScore = englisdScore;
num = index++;//将目前學号賦給目前建立的學生對象,并且将學号自增1,用來賦給下一個建立的學生對象;
}
//計算每個學生的平均成績
public double averScore(){
return Math.round((computerScore + mathScore + englisdScore)/3);// 四舍五入
}
//計算每個學生的總成績
public double sumScore(){
return computerScore + mathScore + englisdScore;
}
//複寫toString()方法,用來輸出學生的資訊
@Override
public String toString() {
return "Student [學号:" + num + ",姓名:" + name + ",年齡:" + age +
",計算機成績:" + computerScore+ ",數學成績:" + mathScore + ",英語成績" + englisdScore +
",各科平均成績:" + averScore() + ",各科總成績:" + sumScore() + "]";
}
//定義各私有成員變量的擷取方法和設定方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getEnglisdScore() {
return englisdScore;
}
public void setEnglisdScore(double englisdScore) {
this.englisdScore = englisdScore;
}
}
練習二:
package sonyi;
/*
練習:
定義一個表示雇員的類。屬性有:雇員編号,姓名,職位,月薪,獎金。
要求雇員編号為類變量,每次增加成員時,編号自動增加,并指派給新增成員。計算雇員的月薪和年薪,列印雇員全部資訊。
執行個體化幾個對象,并測試。
*/
public class EmployeeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//類的執行個體化,并賦初始值
Employee e1 = new Employee("張三","前台",2000,500);
Employee e2 = new Employee("小張","工程師",2000,500);
Employee e3 = new Employee("小李","售後",2000,500);
Employee e4 = new Employee("李四","前台",2000,500);
Employee e5 = new Employee("王五","程式員",8000,1000);
//列印類資訊
System.out.println(e1);
System.out.println(e2);
System.out.println(e3);
System.out.println(e4);
System.out.println(e5);
}
}
class Employee{
//靜态屬性,記錄新增成員編号,并指派給新增成員,指派後自加1
private static int index = 1;
private int no;//定義私有屬性
private String name;
private String position;
private double salary;
private double premium;
Employee(){
}
//重載構造方法
Employee(String name, String position,double salary,double premium){
this.name = name;
this.position = position;
this.salary = salary;
this.premium = premium;
no = index++;
}
//複寫toString,列印成員資訊
@Override
public String toString() {
return "Employee [雇員編号:" + no + ", 姓名:" + name + ", 職位:"
+ position + ", 基本工資:" + salary + ", 獎金" + premium
+ ", 月薪" + monthPay() + ", 年薪:" + annualPay() + "]";
}
//定義私有屬性的擷取方法和設定方法
public double monthPay(){
return salary + premium;
}
public double annualPay(){
return monthPay()*12;
}
//定義私有屬性的擷取方法和設定方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getPremium() {
return premium;
}
public void setPremium(double premium) {
this.premium = premium;
}
}