zoukankan      html  css  js  c++  java
  • 第十一周上机练习

    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();
        }
    
    }
  • 相关阅读:
    UI 简单练习(联动实例)
    软件工程与计算机科学
    中文编程
    自我介绍
    曾经的梦想
    即时通讯研究学习
    即时通讯研究学习
    创业
    2015-08-12-火影
    看<后海不是海>的随想
  • 原文地址:https://www.cnblogs.com/z118127/p/12888590.html
Copyright © 2011-2022 走看看