//1

 public class Area_interface
{
public static void main(String[] args)
{
Rect r = new Rect(15, 12);
Circle c = new Circle(8);
System.out.println(r.getArea());
System.out.println(c.getArea());
}
} interface Areable{
double getArea();
} class NotValueException extends RuntimeException{
NotValueException(String message){
super(message);
}
} // 矩形
class Rect implements Areable{
private int length,width;
public Rect(int length,int width) {
if (length <= 0 || width <= 0){
throw new NotValueException("数值非法!");
}
this.length = length;
this.width = width;
}
public double getArea()
{
return length*width;
}
} // 圆形
class Circle implements Areable{
private int radius;
private static final double PI = 3.14;
public Circle(int radius) {
this.radius = radius;
}
public double getArea()
{
return PI*radius*radius;
}
}

//2

 public int search_char(char[] chs, char key)
{
if(chs==null){
throw new IllegalArgumentException("数组为空");
}
for(int x = 0;x < chs.length;x++){
if(key==chs[x]){
return x;
}
}
return -1;
}

//3

 //first method
int max = 0; //初始化为数组中的任意一个角标
for(int x = 1; x < cir.length; x++){
if(cir[x].radius > cir[max].radius){
max = x;
}
}
return cir[max].radius; //second
double max = cir[0].radius; //初始化为数组任意一个元素的半径
for(int x = 1; x < cir.length; x++){
if(cir[x].radius >max){
max = cir[x].radius;
}
}
return max;

//4

 class Person{
private int age;
private String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
} public void say()
{
System.out.println(name+" "+age);
}
//下面两个都是通过右键搞定的
//get和set方法
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
//hashcode和equals方法
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

equals方法通过eclipse自动生成

//5

 public boolean equals(Object other){
if( !(other instanceof Circle) ){
throw new ClassCastException(other.getClass().getName+"类型错误");
}
Circle c = (Circle)other;
return c.radius==this.radius;
}

day13 面向对象练习的更多相关文章

  1. Java相关英语单词

    day1 Java概述 掌握 .JDK abbr. Java开发工具包(Java Developer's Kit) (abbr.缩写) .JRE abbr. Java运行环境(Java Runtime ...

  2. js下 Day13、面向对象

    一.对象 什么是对象: 一组无序的属性集合 创建对象两种方式: 对象字面量: var obj = {} 实例化: var obj = new Object() 对象取值: **[] ( ** 中括号) ...

  3. Python之路Day13

    day13主要内容:JavaScript.DOM.jQuery 武Sir blog:http://www.cnblogs.com/wupeiqi/articles/5369773.html JavaS ...

  4. python开发学习-day13(js、jQuery)

    s12-20160409-day13 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  5. Python之路day13 web 前端(JavaScript,DOM操作)

    参考链接:http://www.cnblogs.com/wupeiqi/articles/5433893.html day13 1. CSS示例 2. JavaScript 3. DOM操作 上节内容 ...

  6. angular2系列教程(六)两种pipe:函数式编程与面向对象编程

    今天,我们要讲的是angualr2的pipe这个知识点. 例子

  7. 一起学 Java(二)面向对象

    一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...

  8. js面向对象学习 - 对象概念及创建对象

    原文地址:js面向对象学习笔记 一.对象概念 对象是什么?对象是“无序属性的集合,其属性可以包括基本值,对象或者函数”.也就是一组名值对的无序集合. 对象的特性(不可直接访问),也就是属性包含两种,数 ...

  9. 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型

    前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...

随机推荐

  1. Array【数组】和Object【对象】的特性比较

    数组是JavaScript提供的一个内部对象,它是一个标准的集合,我们可以添加(push).删除(shift)里面元素,我们还可以通过for循环遍历里面的元素. 那么除了数组我们在JavaScript ...

  2. sublime text 3 3143注册码

    1.点击help->enter license: —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390 ...

  3. 点击checkbox 向input 里面传值显示

    文本框显示 <input type="text" id="textareashow" name="" class="marg ...

  4. java 散列运算浅分析 hash()

            文章部分代码图片和总结来自参考资料 哈希和常用的方法 散列,从中文字面意思就很好理解了,分散排列,我们知道数组地址空间连续,查找快,增删慢,而链表,查找慢,增删快,两者结合起来形成散列 ...

  5. MongoDB 线上环境按照及配置(授权方式启动)

    1创建文件repo文件 #vim /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Repository bas ...

  6. [android] 与PHP的session进行交互demo

    从MainActivity跳转到MailIndexActivity,第一个请求接口设置session,第二个activity请求接口获取session java代码中获取header头里面的Set-C ...

  7. [android] 界面切换的核心方法

    根据效果图拆分界面 主体部分 View ==> ViewGroup ==> RelativeLayout,主体部分使用RelativeLayout作为占位 View和ViewGroup的区 ...

  8. 定时器实现方式之TimerTask、Timer

    在未来某个指定的时间点或者经过一段时间延迟后执行某个事件,这时候就需要用到定时器了.定时器的实现方式有很多种,今天总结最简单的实现方式.java 1.3引入了定时器框架,用于在定时器上下文中控制线程的 ...

  9. 多版本python如何切换

    一.在命令行中 通过py -x 二.在py文件中 头部字段添加 #!python2 或 #!python3 即可调用相应版本解释器 命令行调用python:py helloworld.py

  10. 实现移动端touch事件的横向滑动列表效果

    要实现手机端横向滑动效果并不难,了解实现的原理及业务逻辑就很容易实现.原理:touchstart(手指按下瞬间获取相对于页面的位置)——>touchmove(手指移动多少,元素相应移动多少). ...