点击这个一键复制代码
观前须知
观看方法,前半为答案,后半为解析(全部题目都为跳进度,所有解析你们应该也看不懂,仅为爱学习的提供,实在不懂可以来问奶糖)
所有####里需要自己修改的都是为了避免数据雷同
第一题
class名为Student
public class Student {
String name = "张三";
int age;
public Student() {
age = 18;
}
public static void main(String args[]) {
Student ### = new Student();
System.out.println("姓名:" + ###.name + ",年龄:" + ###.age);
}
}
将所有‘###’换成自己的变量(改个名字就行)。为了避免重复。
第二题
class名为PeopleDemo
class People {
public String name;
People(String name) {
this.name = name;
}
}
class Student extends People {
String stuID;
Student(String name, String stuID) {
super(name);
this.stuID = stuID;
}
}
public class PeopleDemo {
public static void main(String arg[]) {
Student student = new Student("张三", "20080601");
System.out.println("姓名:" + student.name + ",学号:" + student.stuID);
}
}
解析版
class People {//创建父类
public String name;//名字变量(实例变量)
People(String name) {//构造方法
this.name = name;//名字初始化
}
}
class Student extends People {//创建子类,继承父类的所有可见变量方法。
String stuID;//id变量
Student(String name, String stuID) {//构造方法。
super(name);//将name带到父类,执行构造方法
this.stuID = stuID;
}
}
public class PeopleDemo {
public static void main(String arg[]) {
Student student = new Student("张三", "20080601");
System.out.println("姓名:" + student.name + ",学号:" + student.stuID);
}
}
第3题
class名为Ex3_3
public class Ex3_3 {
public static void main(String[] args) {
Student s1=new Student(##学号##,"##名字##","##性别##",##年龄##);
Student s2=new Student(##学号##,"##名字##","##性别##",##年龄##);
System.out.println("学号:"+s1.show_No()+"\t"+"姓名:"+s1.show_Name()+"\t"+"性别:"+s1.show_Sex()+"\t"+"年龄:"+s1.show_Age());
System.out.println("学号:"+s2.show_No()+"\t"+"姓名:"+s2.show_Name()+"\t"+"性别:"+s2.show_Sex()+"\t"+"年龄:"+s2.show_Age());
s1.modifyAge(##年龄##);
System.out.println("修改后");
System.out.println("学号:"+s1.show_No()+"\t"+"姓名:"+s1.show_Name()+"\t"+"性别:"+s1.show_Sex()+"\t"+"年龄:"+s1.show_Age());
}
}
class Student{
int s_No;
String s_Name;
String s_Sex;
int s_Age;
public String show_Name() {
return s_Name;
}
public int show_No() {
return s_No;
}
public String show_Sex() {
return s_Sex;
}
public int show_Age() {
return s_Age;
}
public void modifyAge(int s_Age) {
this.s_Age = s_Age;
}
public Student(int s_No,String s_Name,String s_Sex,int s_Age) {
this.s_No = s_No;
this.s_Name = s_Name;
this.s_Sex=s_Sex;
this.s_Age=s_Age;
}
}
将##内的替换成自己的数据,输入的类型就是里面的文字提示
解析版
public class Ex3_3 {
public static void main(String[] args) {
Student s1=new Student(##学号##,"##名字##","##性别##",##年龄##);
Student s2=new Student(##学号##,"##名字##","##性别##",##年龄##);//初始化
System.out.println("学号:"+s1.show_No()+"\t"+"姓名:"+s1.show_Name()+"\t"+"性别:"+s1.show_Sex()+"\t"+"年龄:"+s1.show_Age());
System.out.println("学号:"+s2.show_No()+"\t"+"姓名:"+s2.show_Name()+"\t"+"性别:"+s2.show_Sex()+"\t"+"年龄:"+s2.show_Age());//输出
s1.modifyAge(##年龄##);//修改
System.out.println("修改后");
System.out.println("学号:"+s1.show_No()+"\t"+"姓名:"+s1.show_Name()+"\t"+"性别:"+s1.show_Sex()+"\t"+"年龄:"+s1.show_Age());
}
}
class Student{
int s_No;
String s_Name;
String s_Sex;
int s_Age;//四种变量
public String show_Name() {
return s_Name;
}
public int show_No() {
return s_No;
}
public String show_Sex() {
return s_Sex;
}
public int show_Age() {//四种输出变量
return s_Age;
}
public void modifyAge(int s_Age) {//修改age
this.s_Age = s_Age;
}
public Student(int s_No,String s_Name,String s_Sex,int s_Age) {
this.s_No = s_No;
this.s_Name = s_Name;
this.s_Sex=s_Sex;
this.s_Age=s_Age;//初始化
}
}
class Person{
private String name;
private int age;
}
class Student extends Person{
public void study {
System.out.println(student study)
}
public void sayHello( )
}
package week2;
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu =new Student();
stu.setName("张三");
stu.setAge(20);
System.out.println("姓名:"+stu.getName()+"年龄:"+stu.getAge());
}
}