我们讲Java是全英教学讲得还超级快,小弟我就上了了两次课老师布置个作业把我难住了,昨晚忙到两点半还是不会,求大神指点啊!!SOS!
主要想请教Java里是如何创建对象的。
题目翻译成中文如下:
编写一个程序,定义一个表示员工的类Staff,类的属性包括编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)、奖金(prize),员工实发工资(wage)其中员工实发工资=基本工资*出勤率+奖金。定义一个类StaffTest,创建Staff类的对象,通过键盘输入员工基本信息,计算员工实发工资,并输出对象所有属性信息。 java对象教学类
主要想请教Java里是如何创建对象的。
题目翻译成中文如下:
编写一个程序,定义一个表示员工的类Staff,类的属性包括编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)、奖金(prize),员工实发工资(wage)其中员工实发工资=基本工资*出勤率+奖金。定义一个类StaffTest,创建Staff类的对象,通过键盘输入员工基本信息,计算员工实发工资,并输出对象所有属性信息。 java对象教学类
package ch3;import java.util.Scanner;public class CH3_3
{ private int num;
private String name;
private double rateOfAttend;
private int basicSal;
private int prize;
double wage,
wage = basicSal*rateOfAttend+prize;
public static void main(String[] args)
{
public CH3_3(){};
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
public String getNum(){
return num;
}
public void setNum(String num){
this.num=num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getRateOfAttend(){
return rateOfAttend;
}
public void setRateOfAttend(String rateOfAttend){
this.rateOfAttend=rateOfAttend;
}
public String getBasicSal(){
return basicSal;
}
public void setBasicSal(String basicSal){
this.basicSal=basicSal;
}
public String getPrize(){
return prize;
}
public void setPrize(String prize){
this.prize=prize;
}
Scanner input = new Scanner(System.in);
System.out.printf("%s\n","Please input the num of the staff:");
num=in.nextInt();
System.out.printf("%s\n","Please input the name of the staff:");
name=in.nextString();
System.out.printf("%s\n","Please input the rateOfAttend of the staff:");
rateOfAttend=in.nextDouble();
System.out.printf("%s\n","Please input the baiscSal of the staff:");
baiscSal=in.nextInt();
System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();
}}
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
这个方法不能写在main函数下,应该写在构造函数(构造函数和类同名)中,如
CH3_3(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize;
this.wage=wage;
}
还有你的参数名和上面定义的也不一样啊,rateOfAttend定义成double你写成了string
另外System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();这个神马东西?你是先学的c语言吧?
我觉得吧,你可能是不细心,很多错误不该发生
private static String name;
private static double rateOfAttend;
private static int basicSal;
private static int prize;
private static double wage; public Staff() {
}; public Staff(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
Staff.num=num;
Staff.name=name;
Staff.rateOfAttend=rateOfAttend;
Staff.basicSal=basicSal;
Staff.prize=prize;
Staff.wage=wage;
} public int getNum() {
return num;
} public void setNum(int num) {
Staff.num = num;
} public String getName() {
return name;
} public void setName(String name) {
Staff.name = name;
} public double getRateOfAttend() {
return rateOfAttend;
} public void setRateOfAttend(double rateOfAttend) {
Staff.rateOfAttend = rateOfAttend;
} public int getBasicSal() {
return basicSal;
} public void setBasicSal(int basicSal) {
Staff.basicSal = basicSal;
} public int getPrize() {
return prize;
} public void setPrize(int prize) {
Staff.prize = prize;
}
public double getWage(){
return wage;
}
public void setWage(double wage){
Staff.wage=wage;
}
public void displayMessage(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
System.out.printf(" %s%d\n %s%s\n %s%.2f%s\n %s%d\n %s%d\n %s%.2f\n",
"The number of the staff:",num,
"The name of the staff:",name,
"The rate of attend of the staff:",rateOfAttend*100,"%",
"The basicSal of the Staff:", basicSal,
"The prize of the staff:",prize,
"The wage of the staff:",wage);
}
}
package ch3_3;import java.util.Scanner;public class StaffTest { @SuppressWarnings("resource")
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Staff myStaff = new Staff();
System.out.println("Please input the num of the staff:");
int num= input.nextInt();
System.out.println("Please input the name of the staff:");
String name=input.nextLine();
System.out.println();
System.out.println("Please input the rateOfAttend of the staff:");
double rateOfAttend=input.nextDouble();
System.out.println("Please input the baiscSal of the staff:");
int basicSal=input.nextInt();
System.out.println("Please input the prize of the staff:");
int prize=input.nextInt();
double wage = rateOfAttend * basicSal + prize;
myStaff.displayMessage(num,name,rateOfAttend,basicSal,prize,wage);
}}
第二,这样的例子实在太多,你要学会谷歌http://lmgtfy.com/?q=java+simple+class,自己模仿的写一个
你输入num后的回车也会保留在System.in中
会被next.line读到
public class StudentHandle { public static void main(String[] args) {
Student stu1 = new Student("斯蒂芬",110);
stu1.setMathScore(90.0);
stu1.setEnglishScore(80.5);
stu1.setComputerScore(99.5);
System.out.println("斯蒂芬的数学成绩是: "+stu1.getMathScore());
System.out.println("斯蒂芬的英语成绩是: "+stu1.getEnglishScore());
System.out.println("斯蒂芬的计算机成绩是: "+stu1.getComputerScore());
System.out.println("斯蒂芬的成绩总分是: "+stu1.sumScore());
System.out.println("斯蒂芬的成绩平均分是: "+stu1.avgScore());
System.out.println("斯蒂芬的成绩最高分是: "+stu1.maxScore());
System.out.println("斯蒂芬的成绩最低分是: "+stu1.getMathScore());
}
}
/*创建一个学生类*/
class Student{ private String name;//学生的姓名
private int id;//学生的学号
private double mathScore;//数学成绩
private double englishScore;//英语成绩
private double computerScore;//计算机成绩 /*构造方法*/
public Student(String name,int id){
this.name = name;
this.id = id;
} /*学生姓名的get方法*/
public String getName(){return name;} /*学生学号的get方法*/
public int getId(){return id;} /*学生数学成绩的set和get方法*/
public void setMathScore(double mathScore){this.mathScore = mathScore;} public double getMathScore(){return mathScore;}
/*学生英语成绩的set和get方法*/
public void setEnglishScore(double englishScore){this.englishScore = englishScore;} public double getEnglishScore(){return englishScore;} /*学生计算机成绩的set和get方法*/
public void setComputerScore(double computerScore){this.computerScore = computerScore;} public double getComputerScore(){return computerScore;} /*该学生3门课程的成绩总和*/
public double sumScore(){return (mathScore+englishScore+computerScore);} /*该学生3门课程的成绩平均分*/
public double avgScore(){return (mathScore+englishScore+computerScore)/3;} /*该学生3门课程的成绩最高分*/
public double maxScore(){
double maxScore = mathScore;
if(englishScore>maxScore)
maxScore = englishScore;
if(computerScore>maxScore)
maxScore = computerScore;
return maxScore;
} /*该学生3门课程的成绩最低分*/
public double minScore(){
double minScore = mathScore;
if(minScore> englishScore)
minScore = englishScore;
if(minScore> computerScore)
minScore = computerScore;
return minScore;
}
}