7

  1. 创建两个类,分别用来表示长方形和正方形,同时定义所需的成员变量,
    代表长方形或者正方形的边长,在两个类中分别定义两个方法,用来求对应形状的面积和周长,
    并定义相应的get,set方法,获取和改变方形和正方形的变长。
package Q1;

class Square {
    int length;

    public void area() {
        System.out.println("The area is " + length * length);
    }

    public void perimeter() {
        System.out.println("The perimeter is " + 4 * length);
    }

    public void set(int length) {
        this.length = length;
    }

    public int get() {
        return this.length;
    }
}

class Rectangle {
    int length;
    int width;

    public void area() {
        System.out.println("The area is " + length * width);
    }

    public void perimeter() {
        System.out.println("The perimeter is " + 2 * (length + width));
    }

    public void set(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public int getLength() {
        return this.length;
    }

    public int getWidth() {
        return this.width;
    }
}

public class Picture {
    public static void main(String[] args){
        Square s = new Square();
        Rectangle r = new Rectangle();
        s.set(5);
        int sl = s.get();
        System.out.println("The Square's length is " + sl);
        s.area();
        s.perimeter();
        r.set(3,4);
        int rl = r.getLength();
        int rw = r.getWidth();
        System.out.println("The Rectangle's length and width are " + rl + " and " + rw);
        r.area();
        r.perimeter();
    }

}

  1. 定义一个Student类,并要求其他类在使用Student类的时候,最多只能创建10个Student类的对象,如何实现?(就是实现在一个jvm中,最多只能存在10个Student对象)

提示: 首先,要实现该功能,就不能让外部类直接使用
new Student(…)的方式来创建对象,如何不能让其他类new Student(…),
只需将Student类的所 有构造方法的,权限改为private即可。

    接着,把创建对Student对象的工作,交给一个专门的方法去做(想想这个方法应该是怎样的方法)
package Q2;

class Student {
    int id;
    String name;
    String gender;
    int age;

    //静态变量记录新建次数
    static int count = 0;

    private Student() {
    }

    private Student(int id, String name, String gender, int age) {
        set(id, name, gender, age);
    }

    public void set(int id, String name, String gender, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    //静态成员函数控制创建新对象
    public static Student create(int id, String name, String gender, int age) {
        if (count++ < 10)
            return new Student(id, name, gender, age);
        else
            return null;
    }


    public void print() {
        System.out.println("id:" + id + " name:" + name + " gender:" + gender + " age:" + age);
    }
}

public class SuperStudent {
    public static void main(String[] args) {
        Student[] student = new Student[10];
        for (int i = 0; i < 10; i++) {
            student[i] = Student.create(i, "张三", "男", 18);
        }

        //新建第11个对象
        Student stu = Student.create(10, "张三", "男", 18);

        //输出所有对象
        for (int i = 0; i < 10; i++) {
            student[i].print();
        }
        System.out.println(stu);
    }
}

a. 声明一个Person类,里面定义了三个属性,name、gender、age(姓名、性别、年龄)
通过构造方法进行赋值。有一个display方法,可以展示对应的姓名性别年龄信息

b. 声明一个Student类,继承自Person类,增加一个独特的属性id(学号)
通过构造方法进行赋值,同时有一个方法可以展示姓名性别年龄学号信息

c. 声明一个Teacher类,继承自Person类,增加course属性(教学课程)
通过构造方法进行赋值,有一个方法,可以显示姓名性别年龄教学课程信息

d.编写一个测试类,验证你的代码.(分别创建Student对象,和Teacher对象,
要求利用子类对象的显示初始化,即在子类构造方法中,调用父类构造方法的方式
来初始化子类对象。)

package Q3;

class Person {
    private String name;
    private String gender;
    private int age;

    public Person() {}
    public Person(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public void dispaly() {
        System.out.print("name: " + name + " gender: " + " age: " + age);
    }
}

class Student extends Person {
    private int id;

    public Student(String name, String gender, int age, int id) {
        super(name, gender, age);
        this.id = id;
    }

    public void display() {
        super.dispaly();
        System.out.print(" id: " + id);
    }
}

class Teacher extends Person {
    private String course;

    public Teacher(String name, String gender, int age, String course) {
        super(name, gender, age);
        this.course = course;
    }

    public void display() {
        super.dispaly();
        System.out.print(" course: " + course);
    }
}

public class Q3 {
    public static void main(String[] args) {
        Student student = new Student("张三", "男", 18, 01);
        Teacher teacher = new Teacher("李四", "男", 38, "数学");
        student.display();
        System.out.println();
        teacher.display();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值