day13 面向对象练习

2018-01-13 07:55:42来源:cnblogs.com作者:_nbloser人点击

分享
第七城市

//1

 1 public class Area_interface 2 { 3     public static void main(String[] args) 4     { 5         Rect r = new Rect(15, 12); 6         Circle c = new Circle(8); 7         System.out.println(r.getArea()); 8         System.out.println(c.getArea()); 9     }10 }11 12 interface Areable{13     double getArea();14 }15 16 class NotValueException extends RuntimeException{17     NotValueException(String message){18         super(message);19     }20 }21 22 // 矩形23 class Rect implements Areable{24     private int length,width;25     public Rect(int length,int width) {26         if (length <= 0 || width <= 0){27             throw new NotValueException("数值非法!");28         }29         this.length = length;30         this.width = width;31     }32     public double getArea()33     {34         return length*width;35     }36 }37 38 // 圆形39 class Circle implements Areable{40     private int radius;41     private static final double PI = 3.14;42     public Circle(int radius) {43         this.radius = radius;44     }45     public double getArea()46     {47         return PI*radius*radius;48     }49 }

//2

 1 public int search_char(char[] chs, char key) 2 { 3     if(chs==null){ 4         throw new IllegalArgumentException("数组为空"); 5     } 6     for(int x = 0;x < chs.length;x++){ 7         if(key==chs[x]){ 8             return x; 9         }10     }11     return -1;12 }

//3

 1 //first method 2 int max = 0;     //初始化为数组中的任意一个角标 3 for(int  x = 1; x < cir.length; x++){ 4     if(cir[x].radius > cir[max].radius){ 5         max = x; 6     } 7 } 8 return  cir[max].radius; 9 10 //second11 double max = cir[0].radius;    //初始化为数组任意一个元素的半径12 for(int  x = 1; x < cir.length; x++){13     if(cir[x].radius >max){14         max = cir[x].radius;15     }16 }17 return max;

//4

 1 class Person{ 2     private int age; 3     private String name; 4     public Person(int age, String name) { 5         super(); 6         this.age = age; 7         this.name = name; 8     } 9     10     public void say()11     {12         System.out.println(name+"  "+age);13     }14     //下面两个都是通过右键搞定的15     //get和set方法16     public int getAge()17     {18         return age;19     }20     public void setAge(int age)21     {22         this.age = age;23     }24     public String getName()25     {26         return name;27     }28     public void setName(String name)29     {30         this.name = name;31     }32     //hashcode和equals方法33     @Override34     public int hashCode()35     {36         final int prime = 31;37         int result = 1;38         result = prime * result + age;39         result = prime * result + ((name == null) ? 0 : name.hashCode());40         return result;41     }42     @Override43     public boolean equals(Object obj)44     {45         if (this == obj)46             return true;47         if (obj == null)48             return false;49         if (getClass() != obj.getClass())50             return false;51         Person other = (Person) obj;52         if (age != other.age)53             return false;54         if (name == null) {55             if (other.name != null)56                 return false;57         } else if (!name.equals(other.name))58             return false;59         return true;60     }61 }
equals方法通过eclipse自动生成

 

//5

1 public boolean equals(Object other){2     if( !(other  instanceof  Circle) ){3         throw new ClassCastException(other.getClass().getName+"类型错误");4     }5     Circle c = (Circle)other;6     return  c.radius==this.radius;7 }
第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台