zoukankan      html  css  js  c++  java
  • 2020.8.8

    学习内容:

    1、构造函数

    public class Rect {
        private double len,wid;
        Rect(double l,double w) {
            len=l;
            wid=w;
        }
        public double area() {
            return len*wid;
        }
    
        public static void main(String[] args) {
            Rect r=new Rect(3.3,6.1);
            double a=r.area();
            System.out.println("面积为:"+a);
        }
    }

    2、重载

    public class Rect {
        private double len,wid;
        Rect(double l,double w) {
            len=l;
            wid=w;
        }
        Rect(){
            len=10;
            wid=5;
        }
        public double area() {
            return len*wid;
        }
    
        public static void main(String[] args) {
            Rect r1=new Rect(3.3,6.1);
            double a1=r1.area();
            System.out.println("r1面积为:"+a1);
            Rect r2=new Rect();
            double a2=r2.area();
            System.out.println("r2面积为:"+a2);
        }
    }

    3.点类(构造函数重载)

    public class Point2 {
        private double x,y,z;
        private int num;
        Point2(double a,double b,double c,int n){
            x=a;
            y=b;
            z=c;
            num=n;
            System.out.println("构造点类:NO."+num);
        }
        Point2(double a,double b,int n){
            x=a;
            y=b;
            z=1.0;
            num=n;
            System.out.println("构造点类:NO."+num);
        }
        Point2(){
            x=1.0;
            y=1.0;
            z=1.0;
            num=1;
            System.out.println("构造点类:NO."+num);
        }
        public void Output() {
            System.out.println("点的坐标为:("+x+","+y+","+z+")");
        }
        public static void main(String[] args) {
            Point2[] p=new Point2[3];
            p[0]=new Point2();
            p[1]=new Point2(1.0,2.0,3.0,2);
            p[2]=new Point2(6.0,2.0,3);
            for(int i=0;i<3;i++) {
                p[i].Output();
            }
        }
    }

     4.交通工具类

    设计一个交通工具类Vehicle,包含当前载重量和最大载重量两个私有属性,要求具有以下功能和内容:

     

    有两个构造函数(其中一个为默认构造函数);

    可分别设置两个属性的值;

    可判断当前载重量是否符合最大载重量以及加入一定重量后是否超载,若超载发出报警信息;

     

    import java.util.Scanner;
    public class Vehicle {
        private double preh,maxh;
        Vehicle(){
            System.out.println("请输入相关数据(当前重量、最大承载重量):");
            Scanner con=new Scanner(System.in);
            preh=con.nextDouble();
            maxh=con.nextDouble();
        }
        Vehicle(double p,double m){
            preh=p;
            maxh=m;
        }
        public void display() {
            Scanner con=new Scanner(System.in);
            if(preh>maxh)System.out.println("当前质量超过最大重量,超重!");
            else {
                System.out.println("请输入再次加入的重量:");
                double a=con.nextDouble();
                if((preh+a)>maxh)System.out.println("再次加入后超重!");
                else System.out.println("再次加入后没有超重!");
            }
        }
    
        public static void main(String[] args) {
            Vehicle v1=new Vehicle();
            Vehicle v2=new Vehicle(300,500);
            System.out.println("v1的当前重量为:"+v1.preh+"\t v1的最大承载重量为:"+v1.maxh);
            v1.display();
            System.out.println("v2的当前重量为:"+v2.preh+"\t v2的最大承载重量为:"+v2.maxh);
            v2.display();
        }
    }

     
  • 相关阅读:
    MarkDowdPad 破解
    VimFaultException A specified parameter was not correct configSpec.guestId
    did not finish being created even after we waited 189 seconds or 61 attempts. And its status is downloading
    nsx-edge虚拟机抓包实践
    检查子网内存活的主机
    测试防火墙源端口
    vmware创建虚拟机不识别网卡
    ovs-qos配置
    ovs之组网实验
    openstack镜像制作
  • 原文地址:https://www.cnblogs.com/ltw222/p/13463205.html
Copyright © 2011-2022 走看看