定义一个Student类,含类成员变量:
String name、String gender、int score、boolean award,所有的变量为私有(private)。
1.编写有参构造函数:
对name、gender、score、award赋初值;
2. 重写(覆盖)toString方法:
按照格式:类名 [name=, gender=,score=, award=]输出;
3.对每个成员变量生成setter/getter方法;
4.main方法中创建对象并输出。
输入格式:
输入1行学生的信息:姓名、性别、成绩、获奖情况。
输出格式:
通过对象调用toString方法输出学生的姓名、性别、成绩、获奖情况信息。
输入样例:
Rose female 96 true
输出样例:
Student [name=Rose, gender=female, score=96, award=true]
import java.util.Scanner;
class Student
{
private String name;
private String gender;
private int score;
private boolean award;
public Student(String n,String g,int s,boolean a)
{
name=n;
gender=g;
score=s;
award=a;
}
public String toString()
{
return "Student [name="+this.name+", gender="+this.gender+", score="+this.score+", award="+this.award+"]";
}
public void setname(String name)
{
this.name=name;
}
public String getname()
{
return name;
}
public void setgender(String gender)
{
this.gender=gender;
}
public String getgender()
{
return gender;
}
public void setscore(int score)
{
this.score = score;
}
public int getscore()
{
return score;
}
public void setaward(boolean award)
{
this.award=award;
}
public boolean getaward()
{
return award;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String name=sc.next();
String gender=sc.next();
int score=sc.nextInt();
boolean award=sc.nextBoolean();
Student s=new Student(name,gender,score,award);
System.out.println(s);
}
}
标签:Java,定义,创建,award,name,score,gender,public,String
From: https://www.cnblogs.com/aal-izz-well-eureka/p/16826194.html