day13 面向对象练习
//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 面向对象练习的更多相关文章
- Java相关英语单词
day1 Java概述 掌握 .JDK abbr. Java开发工具包(Java Developer's Kit) (abbr.缩写) .JRE abbr. Java运行环境(Java Runtime ...
- js下 Day13、面向对象
一.对象 什么是对象: 一组无序的属性集合 创建对象两种方式: 对象字面量: var obj = {} 实例化: var obj = new Object() 对象取值: **[] ( ** 中括号) ...
- Python之路Day13
day13主要内容:JavaScript.DOM.jQuery 武Sir blog:http://www.cnblogs.com/wupeiqi/articles/5369773.html JavaS ...
- python开发学习-day13(js、jQuery)
s12-20160409-day13 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Python之路day13 web 前端(JavaScript,DOM操作)
参考链接:http://www.cnblogs.com/wupeiqi/articles/5433893.html day13 1. CSS示例 2. JavaScript 3. DOM操作 上节内容 ...
- angular2系列教程(六)两种pipe:函数式编程与面向对象编程
今天,我们要讲的是angualr2的pipe这个知识点. 例子
- 一起学 Java(二)面向对象
一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...
- js面向对象学习 - 对象概念及创建对象
原文地址:js面向对象学习笔记 一.对象概念 对象是什么?对象是“无序属性的集合,其属性可以包括基本值,对象或者函数”.也就是一组名值对的无序集合. 对象的特性(不可直接访问),也就是属性包含两种,数 ...
- 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
随机推荐
- 为easyui添加多条件验证
easyui的验证框架,validatebox不能有效的支持多个条件的验证,比如中文用户名,既要验证其是中文,又要验证其长度不超过6位时便显得很繁琐,需要反复的为easyui添加验证规则. 在此实现一 ...
- python2.7 输入&函数参数&路径表示&各种下标_含义
1.Python2.x与3.x的input区别 input与python3不同,在python2.7中分为input()与raw_input() 其中input()返回的是int/float类型数据, ...
- CCF 201412-4 最优灌溉
问题描述 试题编号: 201412-4 试题名称: 最优灌溉 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很 ...
- Java中break、continue及标签等跳转语句的使用[下]
作为上一篇使用for循环演示的跳转,这一篇将使用while.相比较来说,while比for循环更简单.代码如下: public class LabeledWhile { public static v ...
- [PY3]——求TopN/BtmN 和 排序问题的解决
需求 K长的序列,求TopN K长的序列,求BtmN 排序问题 解决 heap.nlargest().heap.nsmallest( ) sorted( )+切片 max( ).min( ) 总结和比 ...
- 【HTML基础】常用基础标签
什么是HTML? HTML(HyperText Markup Language,超文本标记语言),所谓超文本就是指页面内可以包含图片.链接.甚至音乐等非文字元素,HTML不是一种编程语言,而是一种标记 ...
- 开发常用的 JavaScript 知识点总结
No1.语法和类型 1.声明定义 变量类型:var,定义变量:let,定义块域(scope)本地变量:const,定义只读常量.变量格式:以字母.下划线“_”或者$符号开头,大小写敏感.变量赋值:声明 ...
- .netCore2.0 依赖注入
依赖注入(ID)是一种实现对象及其合作者或者依赖想之间松散耦合的技术对于传统的方法来说,获取类的方法通常用new如下 public class DIController : Controller { ...
- 分析apache日志,统计访问量
cat nondomain_access_log.20090722 |awk '{print $1}'| sort | uniq -c |sort -nr
- Hadoop实战之一~Hadoop概述
对技术,我还是抱有敬畏之心的. Hadoop概述 Hadoop是一个开源分布式云计算平台,基于Map/Reduce模型的,处理海量数据的离线分析工具.基于Java开发,建立在HDFS上,最早由Goog ...