1.创建一个学生类Student,包含:
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
2.编写上题Studentt类的测试程序
(1)创建2个具体学生对象;
(2)使每个学生年龄加1;
(3)判断学生年龄是否小于20,是则打印输出相关信息。
3.增加1个学校名schoolName。 一个setSchool方法设置校名, 统计学校人数并输出。
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
2.编写上题Studentt类的测试程序
(1)创建2个具体学生对象;
(2)使每个学生年龄加1;
(3)判断学生年龄是否小于20,是则打印输出相关信息。
3.增加1个学校名schoolName。 一个setSchool方法设置校名, 统计学校人数并输出。
private String sex;
private int age;
private static int count;
private String schoolName;
public Student(String name,String sex,int age) {
this.name = name;
this.sex = sex;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String getSchoolName() {
return schoolName;
} public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
} public void outInfo(String name, String sex, int age){
name = getName();
sex = getSex();
age = getAge();
System.out.println("姓名:" + name + " 年龄:" +age + " 性别:" +sex);
}
public int addAge(int age){
age = getAge();
age++;
return age;
}
{
count++;
}
/**
* @param args
*/
public static void main(String[] args) {
Student stu1 = new Student("1","2",3);
Student stu2 = new Student("2","2",4);
stu1.setSchoolName("大学");
String school = stu1.getSchoolName();
System.out.println(school + " 学校共有:" + count +"名学生");
int age = stu1.getAge();
String name = stu1.getName();
String sex = stu1.getSex();
if(age < 20){
System.out.println("学生姓名:" + name + "学生年龄:" + age + "学生性别: " + sex);
}
}}
本人新手 如有问题请见谅
private String name ;
private String sex ;
private static int age ;
private static count ;
private String schoolName ;
public Student(String name , String sex , int age){
this.name = name ;
this.sex = sex ;
this.age = age ;
count ++ ;
}
public void setName(String name){
this.name = name ;
}
public void setSex(String sex){
this.sex = sex ;
}
public void setAge(int age){
this.age = age ;
}
public String outinfo(){
return "学生信息:"+"\n"+
"\t -|学生姓名:"+this.name+
"\t -|学生性别:"+this.sex+
"\t -|学生年龄:"+this.age
;
}
public String addAge(){
age++ ;
if(age < 20){
return "年龄小于20岁" ;
}
else{
return "年龄大于或者20岁" ;
}
}
public static void setSchoolName(String schoolName){
this.schoolName = schoolName ;
System.out.println("学校的名称:"+this.schoolName+"学生的人数:"+Student.count);
}
}
public class OODemo{
public static void main(String args[]){
Student stu1 = new Student("张三","男",18) ;
System.out.println(stu1.addAge()) ;
Student stu2 = new Student("李四","女",19) ;
System.out.println(stu2.addAge()) ;
Student.setSchoolName("山东师范大学长清校区") ;
}
}
1.创建一个学生类Student,包含:
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
如果真不会,可以搜“JAVA 构造方法”,总能搜出示例来的。照猫画虎就是了。
http://www.xzbu.com/qkxx-338.htm
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
2.编写上题Studentt类的测试程序
(1)创建2个具体学生对象;
(2)使每个学生年龄加1;
(3)判断学生年龄是否小于20,是则打印输出相关信息。
3.增加1个学校名schoolName。 一个setSchool方法设置校名, 统计学校人数并输出。*/
class Student
{
String name;
String sex;
int age;
Student(String name,String sex,int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String _name)
{
name = _name;
}
public String getName()
{
return name;
}
public void setSex(String _sex)
{
sex = _sex;
}
public String getSex()
{
return sex;
}
public void setAge(int _age)
{
age = _age;
}
public int getAge()
{
return age;
}
public void outInfo()
{
System.out.println("这名学生的信息为:"+name+" "+sex+" "+age);
}
}public class Studentt {
public static void main(String[] args)
{
Student st1 = new Student("张三","男",16);
Student st2 = new Student("李四","女",17);
st1.setAge(16+1);
if(st1.getAge()<20)
st1.outInfo();
st2.setAge(17+1);
if(st2.getAge()<20)
st2.outInfo();
}
}
做到问题2,问题3还没做
上面的程序也没有优化的
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
2.编写上题Studentt类的测试程序
(1)创建2个具体学生对象;
(2)使每个学生年龄加1;
(3)判断学生年龄是否小于20,是则打印输出相关信息。
3.增加1个学校名schoolName。 一个setSchool方法设置校名, 统计学校人数并输出。*/
class Student
{
String name,schoolName;
String sex;
int age;
static int count=0;
Student(String name,String sex,int age,String school)
{
this.name = name;
this.sex = sex;
this.age = age;
this.schoolName =schoolName;
}
public void setName(String _name)
{
name = _name;
}
public String getName()
{
return name;
}
public void setSex(String _sex)
{
sex = _sex;
}
public String getSex()
{
return sex;
}
public void setAge(int _age)
{
age = _age;
}
public int getAge()
{
return age;
}
public void setSchoolName(String schoolName)
{
this.schoolName = schoolName;
}
public String getSchoolName()
{
return schoolName;
}
public void outInfo()
{
System.out.println("这名学生的信息为:"+name+" "+sex+" "+age+" "+schoolName);
}
{count++;}
public static void main(String[] args)
{
Student st1 = new Student("张三","男",16,"SC1");
Student st2 = new Student("李四","女",17,"SC2");
st1.setAge(16+1);
if(st1.getAge()<20)
st1.outInfo();
st2.setAge(17+1);
if(st2.getAge()<20)
st2.outInfo();
System.out.println("学校的人数为:"+count);
}
}
private String name;
private String sex;
private int age;
Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
} public void setName(String name) {
this.name = name;
} public void setSex(String sex) {
this.sex = sex;
} public void setAge(int age) {
this.age = age;
} public void outInfo(){
System.out.println("姓名:"+name+" 性别:"+sex+" 年龄:"+age);
}
public static void fun(Student stu){
if(stu.age<20){
stu.outInfo();
}
} public static void main(String[] args) {
Student stu1=new Student("张三","男",17);
Student stu2=new Student("小五","女",19);
stu1.age++;
stu2.age++;
fun(stu1);
fun(stu2);
}
}
紧急 [ 标签:java, student, 研究生 ] 遇见在三月的 离问题结束还有14天22小时
Student类要求:1.创建一个学生类Student,包含:
3个属性:name,sex,age。
1个构造方法:有3个参数的构造方法,用于对name、sex和age属性初始化;
4个方法:方法setName, setAge; getAge;
outInfo()输出学生信息。
2.编写上题Studentt类的测试程序
(1)创建2个具体学生对象;
(2)使每个学生年龄加1;
(3)判断学生年龄是否小于20,是则打印输出相关信息。
3.增加1个学校名schoolName。 一个setSchool方法设置校名, 统计学校人数并输出。GraduateStudent要求:(1)为其添加:两个属性:专业speciality和导师teacher;
1个构造方法:带有5个参数的构造器方法,
(2)重写Student类中的方法outInfo(),输出研究生相关信息。