zoukankan      html  css  js  c++  java
  • Java继承练习题

    第一题

    建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求:  汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法

    第二题

    写一个Person类,有编号id,姓名name,职位job

    构造方法带三个参数。

    方法:

    登陆login

    注册register

    自我介绍talk

    写一个学生Student类继承Person类,方法有:考试test

    属性有:学费money

    写一个老师Teacher类继承Person类,

    属性有 工资salary

    方法有:工作work

    写一个测试类TestPerson,测试学生和老师

    学生:姓名-张三  职位-学生  学费-18000

    老师:姓名-李四  职位-老师  工资-8000

    注册、登陆输出就可以,自我介绍输出属性

    第三题

    定义一个父类:形状类Shapes,里面有两个方法,分别是求面积和周长。

    定义一个子类:矩形Rectangle

    定义一个子类:三角形 Triagle

    定义一个子类:圆 Circle

    定义一个测试类:传入圆的半径4 输出周长和面积

                    传入矩形的长和宽45 输出周长和面积

                    传入三角形三边:345 输出周长和面积

    第一题:
    package homework;
    
    public class Auto {
        protected int gears;
        protected String color;
        protected double weight;
        protected double speed;
        
        public Auto() {
            this.gears=4;
            this.color="黄色";
            this.weight=200;
            this.speed=50;
        }
    
        public Auto(int gears, String color, double weight, double speed) {
            super();
            this.gears = gears;
            this.color = color;
            this.weight = weight;
            this.speed = speed;
        }
        public void speedUp()
        {
            speed+=5;
            System.out.println("加速至"+speed);
        }
        public void speedDown()
        {
            speed-=5;
            System.out.println("减速至"+speed);
        }
        public void stop()
        {
            speed=0;
            System.out.println("汽车停止,速度是"+speed);
        }
        
    }
    
    package homework;
    
    public class Car extends Auto{
        protected String kongtiao;
        protected String CD;
        
        public Car() {
            super();
            kongtiao="品牌1空调";
            CD="CD1";
        }
        
        public Car(int gears, String color, double weight, double speed,String kongtiao, String cD) {
            super(gears, color, weight, speed);
            this.kongtiao = kongtiao;
            CD = cD;
        }
        
        @Override
        public void speedUp(){
            super.speed+=10;
            System.out.println("加速至"+speed);
        }
        @Override
        public void speedDown() {
            speed-=10;
            System.out.println("减速至"+speed);
        }
        
        public void information()
        {
            System.out.println("当前是"+color+"的"+gears+"轮"+color+"的重"+weight+"的配有"+kongtiao+"和"+CD+"的车子,当前时速为"+speed);
        }
        
    }
    package homework;
    
    public class TestAuto {
        public static void main(String[] args) {
            Car car=new Car();
            car.information();
            car.speedDown();
            car.speedUp();
            car.stop();
            System.out.println();
            Car car2=new Car(3, "白色", 300, 70,"品牌2空调", "CD2");
            car2.information();
            car2.speedDown();
            car2.speedUp();
            car.stop();
        }
    }
    
    
    
    第二题:
    package homework;
    
    public class Person {
        protected int id;
        protected String name;
        protected String job;
        
        public Person(int id, String name, String job) {
            this.id = id;
            this.name = name;
            this.job = job;
        }
        public void login()
        {
            System.out.println("登陆成功");
        }
        public void register()
        {
            System.out.println("注册成功");
        }
        public void talk()
        {
            System.out.println("大家好!我是"+name+",编号是"+id+",职位是"+job);
        }
    }
    
    package homework;
    
    public class Student extends Person {
        protected double money;
        
        public Student(int id, String name, String job, double money) {
            super(id, name, job);
            this.money = money;
        }
    
        public void test()
        {
            System.out.println("我在考试");
        }
        public void information()
        {
            System.out.println("姓名-"+name+"	职位-"+job+"	学费"+money);
        }
        
    }
    
    package homework;
    
    public class Teacher extends Person{
        protected double salary;
        
        public Teacher(int id, String name, String job, double salary) {
            super(id, name, job);
            this.salary = salary;
        }
    
        public void work()
        {
            System.out.println("我在工作");
        }
        public void information()
        {
            System.out.println("姓名-"+name+"	职位-"+job+"	工资"+salary);
        }
    }
    package homework;
    
    public class TestPerson {
    
        public static void main(String[] args) {
            Student stu=new Student(1, "张三", "学生", 18000);
            stu.login();
            stu.register();
            stu.talk();
            stu.test();
            stu.information();
            System.out.println();
            Teacher tea=new Teacher(2, "张老师", "老师", 8000);
            tea.login();
            tea.register();
            tea.talk();
            tea.information();
        }
    
    }
    
    
    
    第三题:
    package homework;
    
    public class Shapes {
        public void area()
        {
            System.out.println("求面积");
        }
        public void zhouChang()
        {
            System.out.println("求周长");
        }
    }
    package homework;
    
    public class Rectangle extends Shapes {
        private int chang;
        private int kuan;
        
        public Rectangle(int chang, int kuan) {
            super();
            this.chang = chang;
            this.kuan = kuan;
        }
        @Override
        public void area() {
            System.out.println("长方形面积是"+chang*kuan);
        }
        @Override
        public void zhouChang() {
            System.out.println("长方形周长是"+(chang+kuan)*2);
        }
        
    }
    package homework;
    
    public class Circle extends Shapes{
        private int a;
    
        public Circle(int a) {
            super();
            this.a = a;
        }
        @Override
        public void area() {
            System.out.println("圆形面积是"+a*a*Math.PI);
        }
        @Override
        public void zhouChang() {
            System.out.println("圆形周长是"+a*Math.PI*2);
        }
        
        
        
    }
    package homework;
    
    public class Triagle extends Shapes {
        private int a;
        private int b;
        private int c;
    
        
        public Triagle(int a, int b, int c) {
            super();
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public void area() {
            System.out.println("三角形面积是"+Math.sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))/4);
        }
        @Override
        public void zhouChang() {
            System.out.println("三角形周长是"+(a+b+c));
        }
    }
    package homework;
    
    public class TestShapes {
    
        public static void main(String[] args) {
            Circle circle=new Circle(1);
            circle.area();
            circle.zhouChang();
            Rectangle rectangle=new Rectangle(2, 3);
            rectangle.area();
            rectangle.zhouChang();
            Triagle triagle=new Triagle(3, 4, 5);
            triagle.area();
            triagle.zhouChang();
        }
    
    }
  • 相关阅读:
    springboot配置文件priperties大全
    JWT全面解读、使用步骤
    springboot+jwt做api的token认证
    SpringSecurity-UsernamePasswordAuthenticationFilter的作用
    java中字符串太长,怎么自动换到下一行
    idea中mybatis generator自动生成代码配置 数据库是sqlserver
    Spring REST实践之HATEOAS
    在eclipse中查看sources源码和JavaDoc帮助文档
    在Eclipse中查看Javadoc文档
    【layui】laydate的年度类型设值,不能使用done方法时,翻页不设值
  • 原文地址:https://www.cnblogs.com/lumc5/p/15129905.html
Copyright © 2011-2022 走看看