zoukankan      html  css  js  c++  java
  • 用java编写一个函数,用于计算桌子的面积,可计算任意边长的桌子

    /*
     *桌子实体类,有属性和方法
     */
    public class Table {
        String name; // 声明桌子名称
        Double width; // 声明桌子宽度
        Double length; // 声明桌子长度
        
        //无参构造函数
        public Table() {
            
        }
        //定义有参构造方法,为属性初始化
        public Table(Double width,Double length) {
            this.width=width;
            this.length=length;
        }
        //带参构造函数
        public Table(String name,Double width,Double length) {
            this.name=name;
            this.width=width;
            this.length=length;
        }
        //设置,返回对象属性,Soure ,get,set方法封装属性
        public String getName() {
            return name;        //返回桌子对象名字
        }

        public void setName(String name) {
            this.name = name; //设置桌子名字属性
        }

        public Double getWidth() {
            return width;
        }

        public void setWidth(Double width) {
            this.width = width;
        }

        public Double getLength() {
            return length;
        }

        public void setLength(Double length) {
            this.length = length;
        }
        //计算桌子面积
        public double Area() {
            double area = 0;// area:桌面面积
            area = width * length;
            return area;
        }
        //无参构造方法,输出所有属性
        public void Display() {
            System.out.println("桌子名称为:" + name + " 桌面宽度为:" + width + " 米" + " 桌面长度为:" + length + " 米");
            System.out.println("桌面面积为:" + this.Area() + " 平方米");
        }
    }

    public class TableTest {
        //程序入口,main()方法只能有一个,static静态修饰符
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //Table table=null;   //声明Table类对象table
            //table=new Table();  //实例化Table类对象table
            //声明并实例化Table类对象table1
            Table table1=new Table();
            table1.name="方桌";
            
            table1.width=3.5;
            table1.length=4.5;
            table1.Area();
            table1.Display();
            
            Table table2=table1;  //将table1赋给table2
            table2.Area();
            table2.Display();
            
            //匿名对象 只能使用一次,new关键字进行开辟,使用的是堆内存,没有对应的栈内存空间引用
            new Table("正方桌1",2.0,2.0).Area();
            //new Table("正方桌2",3.0,3.0).Display();
            Table table3=new Table("方桌3",2.4,2.6);//声明并实例化对象table3
            table3.Area();
            table3.Display();
            
            //通过静态方法取得类的实例
            SingletonClass scl=SingletonClass.getInstance();
            //通过静态方法取得类的实例
            SingletonClass sc2=SingletonClass.getInstance();
            //通过静态方法取得类的实例
            SingletonClass sc3=SingletonClass.getInstance();
            scl.show();
            sc2.show();
            sc3.show();
        }

    }

  • 相关阅读:
    LeetCode Missing Number (简单题)
    LeetCode Valid Anagram (简单题)
    LeetCode Single Number III (xor)
    LeetCode Best Time to Buy and Sell Stock II (简单题)
    LeetCode Move Zeroes (简单题)
    LeetCode Add Digits (规律题)
    DependencyProperty深入浅出
    SQL Server存储机制二
    WPF自定义RoutedEvent事件示例代码
    ViewModel命令ICommand对象定义
  • 原文地址:https://www.cnblogs.com/TangGe520/p/8747395.html
Copyright © 2011-2022 走看看