1.定义一个学生类(Student),属性有private的名字(name), public的年龄(age),专业(specialty)和封装name属性的方法:setName(),getName()



 class student{
private String name;
public String specialty;
public int age;
public student(String name,int age,String specialty) {
this.name=name;
this.age=age;
this.specialty=specialty;
}

public void setname(String name) {
this.name=name;
}
public String getname() {
return name;
}
public String getspecialty() {
return specialty;
}


}


public class Text_stu {
public static void main(String args[]) {
student s=new student("lizheng",19,"");
s.setname("lizheng");
s.age=19;
printthis(s);
}
static 
public void printthis(student s) {
System.out.println("mame is "+s.getname());
System.out.println("age is "+s.age);
}
}
Sure, I'd love to answer your question about Java programming. Here's an implementation of the Student class and its derived classes: ``` public class Student { protected String name; protected int age; protected String degree; public Student(String name, int age, String degree) { this.name = name; this.age = age; this.degree = degree; } public void show() { System.out.printf("Name: %s\nAge: %d\nDegree: %s\n", name, age, degree); } } public class Undergraduate extends Student { private String specialty; public Undergraduate(String name, int age, String degree, String specialty) { super(name, age, degree); this.specialty = specialty; } public void show() { super.show(); System.out.printf("Specialty: %s\n", specialty); } } public class Graduate extends Student { private String direction; public Graduate(String name, int age, String degree, String direction) { super(name, age, degree); this.direction = direction; } public void show() { super.show(); System.out.printf("Direction: %s\n", direction); } } ``` Here's how you can use these classes to create instances and display their information: ``` public static void main(String[] args) { Student s1 = new Student("John", 20, "Bachelor"); s1.show(); // Name: John\nAge: 20\nDegree: Bachelor Student s2 = new Undergraduate("Alice", 19, "Bachelor", "Computer Science"); s2.show(); // Name: Alice\nAge: 19\nDegree: Bachelor\nSpecialty: Computer Science Student s3 = new Graduate("Bob", 25, "Master", "Artificial Intelligence"); s3.show(); // Name: Bob\nAge: 25\nDegree: Master\nDirection: Artificial Intelligence } ``` I hope this helps! Let me know if you have any more questions about Java programming.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值