java 定义一个类student,要求包括学号id,姓名name,年龄age三个属性

2024-11-27 13:39:47
推荐回答(2个)
回答(1):

第一题
class Student{//student类
//属性
public int id;
public String name;
public String age;
//方法
void learn(){System.out.println(name+"正在学习");}
void practice(){System.out.println(name+"正在实践");}
}

第二题
Student s1 = new Student();
//属性
s1.age = 16;
s1.name = "张三";
s1.id = 1;
//方法
s1.learn();
si.practice();

第三题
Student(String name)//一个参数
{this.name=name;}
Student(String name, int age)//两个参数
{this.age=age;this.name=name;}
Student(int id,String name,int age)//三个参数
{ this.id=id;this.name=name;this.age=age;}
就这么就行了,还需要细分么?

回答(2):

package com.ufotable.test;
public class Teat29 {
public static void main(String[] args) {
Student s= new Student("200918241237", "ufotable", 22);
s.learn("JAVA");
s.practice("SSH");
}
}
class Student{
Student(String name){this.name=name;}
Student(String id,String name){this.id=id;this.name=name;}
Student(String id,String name,int age){this.id=id;this.name=name;this.age=age;}
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String id;
void learn(String names){System.out.println(name+"正在学习"+names+"。");}
void practice(String names){System.out.println(name+"正在实践"+names);}
}

输出结果:

ufotable正在学习JAVA。
ufotable正在实践SSH