• 第十一周上机练习


    1.

    package test;
    public class Vehicle {
         public String brand;
            public String color;
            public double speed=0;
            void setVehicle(String brand,String color) {
                this.brand=brand;
                this.color=color;
            }
            void access(String brand,String color,double speed) {
                this.brand=brand;
                this.color=color;
                this.speed=speed;
            }
            void run() {
            System.out.println("车的品牌是"+this.brand+" 颜色是"+this.color+" 速度是"+this.speed);
            }
    }
    package test;
    public class VehicleTest {
         public static void main(String[] args) {
             Vehicle m=new Vehicle();
                m.access("a" ,"black", 450);
                m.run();
         }
    }
    package test;
    public class Car extends Vehicle {
         int loader;
            void access(String brand,String color,double speed,int loader) {
                this.brand=brand;
                this.color=color;
                this.speed=speed;
                this.loader=loader;
            }
            void run() {
                System.out.println("车的品牌是"+this.brand+" 颜色是"+this.color+" 速度是"+this.speed+" 载人数是"+this.loader);
            }
    }
    package test;
    public class LH {
        public static void main(String[] args) {
             Car n=new Car();
                n.access("b", "red", 330, 4);
                n.run();
        }
    }

    2.

    package test;
    public abstract class Shape {
        protected double area;
        protected double per;
        protected String color;
    
        public Shape() {
        }
    
        public Shape(String color) {
            this.color = color;
        }
    
        public abstract void getArea();
    
        public abstract void getPer();
    
        public abstract void showAll();
    
    }
    package test;
    public class Rectangle extends Shape {
        double width;
        double height;
    
        public Rectangle() {
        }
    
        public Rectangle(double width, double height, String color) {
            super();
            this.width = width;
            this.height = height;
            this.color = color;
        }
    
        public void getArea() {
            area = width * height;
        }
    
        public void getPer() {
            per = (width + height) * 2;
        }
    
        public void showAll() {
            System.out.println("矩形面积为:" + area + ",周长为:" + per + ",颜色为:" + color);
        }
    }
    package test;
    public class Circle extends Shape {
        double radius;
    
        public Circle() {
        }
    
        public Circle(double radius, String color) {
            this.color = color;
            this.radius = radius;
        }
    
        public void getArea() {
            area = radius * radius * 3.14;
        }
    
        public void getPer() {
            per = 2 * radius * 3.14;
        }
    
        public void showAll() {
            System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色为:" + color);
        }
    }
    package test;
    public class PolyDemo {
    
        public static void main(String[] args) {
            Shape a = new Circle(4, "蓝色");
            Shape b = new Rectangle(6, 8, "黑色");
            a.getArea();
            a.getPer();
            a.showAll();
            b.getArea();
            b.getPer();
            b.showAll();
        }
    
    }
  • 相关阅读:
    HTML大文件上传(博客迁移)
    微信小程序初探
    基于NodeJS微信公众号
    基于NodeJS的秘室聊天室
    CSS3 值得称赞新特性
    CAS FOR WINDOW ACTIVE DIRECTORY SSO单点登录
    IOS学习之-私人通讯录
    android 模拟2048
    使用ctypes在Python中调用C++动态库
    [Leetcode] Longest Palindromic Subsequence
  • 原文地址:https://www.cnblogs.com/z118127/p/12888590.html
Copyright © 2020-2023  润新知