Java 构造器(构造方法)练习

Java  构造器(构造方法)练习

 

 

 1 package com.bytezero.triangle;
 2 
 3 public class TriAngle 
 4 {
 5     //私有属性
 6     private double base; //边长
 7     private double height;  //高
 8     
 9     //构造器
10     public TriAngle()
11     {
12         
13     }
14     
15     public TriAngle(double b,double h)
16     {
17         base = b;
18         height = h;
19     }
20     
21     //方法
22     public void setBase(double b)
23     {
24         base = b;
25     }
26     
27     public double getBase()
28     {
29         return base;
30     }
31     
32     public void setHeight(double h)
33     {
34         height = h;
35     }
36     public double getHeight()
37     {
38         return height;
39     }
40     
41     
42     
43 }
 1 package com.bytezero.triangle;
 2 
 3 public class TriAngleTest 
 4 {
 5     public static void main(String[] args)
 6     {
 7         TriAngle t1 = new TriAngle();
 8         t1.setBase(2.2);
 9         t1.setHeight(3.3);
10         System.out.println("base :"+t1.getBase()+",  height:"+t1.getHeight());
11     
12     
13         TriAngle t2 = new TriAngle(6.6,7.7);
14         System.out.println("base :"+t2.getBase()+",  height:"+t2.getHeight());
15     
16     
17     
18     }
19 }

Java  构造器(构造方法)练习

 

上一篇:Pandas


下一篇:当运行docker run -i -t ubuntu /bin/bash时,提示报错Error response from daemon: EOF?