• 类中定义成员方法。加不加public有什么区别?


      1 class Trangle{
      2     double sideA, sideB, sideC, area, length;
      3     boolean flag;
      4     Trangle(double a, double b, double c){//public
      5         if(a < (b + c) && b < (a + c) && c < (a + b)){
      6             sideA = a;
      7             sideB = b;
      8             sideC = c;
      9             flag = true;
     10         }
     11         else
     12             flag = false;
     13     }
     14     double getLength(){//public
     15         length = sideA + sideB + sideC;
     16         return length;
     17     }
     18     double getArea(){//public
     19         if(flag){
     20             double p = (sideA + sideB + sideC)/2.0;
     21             area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
     22             return area;
     23         }
     24         else{
     25             System.out.println("This is not a trangle");
     26             return 0;
     27         }
     28     }
     29     void setABC(double a, double b, double c){//public
     30         if(a < (b + c) && b < (a + c) && c < (a + b)){
     31             sideA = a;
     32             sideB = b;
     33             sideC = c;
     34             flag = true;
     35         }
     36         else{
     37             flag = false;
     38             sideA = sideB =sideC = 0;        
     39         }
     40     }    
     41 }
     42 
     43 class Lader{
     44     double above,botton,height,area;
     45     Lader(double a,double b, double h){
     46         above =a;
     47         botton = b;
     48         height = h;
     49     }
     50     public double getArea(){
     51         area = (above + botton)*height/2.0;
     52         return area;
     53     }
     54 }
     55 
     56 class Circle{
     57     double radius, area;
     58     Circle(double r){
     59         if(r > 0)
     60             radius = r;
     61         else 
     62             radius = 0;
     63     }
     64     double getRadius(){
     65         return radius;
     66     }
     67     double getArea(){
     68         area = Math.PI * radius * radius;
     69         return area;
     70     }
     71     double getLength(){
     72         return 2 * Math.PI * radius;
     73     }
     74     void setRadius(double r){
     75         if(r > 0)
     76             radius = r;
     77         else 
     78             radius = 0;
     79     }
     80 }
     81 
     82 public class AreaAndLength{
     83     public static void main(String[] args){
     84         double length, area;
     85         Circle circle = null;
     86         Trangle trangle = null;
     87         Lader lader = null;
     88         circle = new Circle(2.0);
     89         trangle = new Trangle(5.0, 3.0, 4.0);
     90         lader = new Lader(4, 8, 5);
     91         
     92         length = circle.getLength();
     93         System.out.print("
    Circle's length is "+ length);    
     94         area = circle.getArea();
     95         System.out.println(". Area is "+ area +".
    ");
     96         
     97         length = trangle.getLength();
     98         System.out.print("Trangle's length is "+ length);    
     99         area = trangle.getArea();
    100         System.out.println(". Area is "+ area +".
    ");
    101         
    102         area = lader.getArea();
    103         System.out.println("Lader's area is "+ area +".
    ");
    104         
    105         trangle.setABC(12, 34, 1);
    106         area = trangle.getArea();
    107         System.out.print("This nwe trangle's area is "+ area);
    108         length = trangle.getLength();
    109         System.out.println(". Length is "+ length +".
    ");    
    110     }
    111 }

    trangle类的成员函数加不加public都可以运行成功,什么原因呢?

  • 相关阅读:
    鼠标移入图片放大效果
    搜索框
    将下拉菜单放入input框中
    轮播图2
    V-demo item.vue
    v-demo1 List.vue
    v-demo1 add.vue
    v-demo1 app.vue
    3D轮播图
    封装代码
  • 原文地址:https://www.cnblogs.com/lixiaoxu/p/7573291.html
Copyright © 2020-2023  润新知