//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. 运行时用AnimatorOverrideController动态加载动画片段

    https://blog.csdn.net/tlrainty/article/details/54602786 项目中经常会遇到这种情况:很多模型动画的AnimatorController是一模一样的 ...

  2. JavaScript中的异步 macrotask 和 microtask

    看过很多setTimeout.Promise执行顺序的面试题,一直不明白为啥都是异步操作,Promise就牛×些呢?直到了解了macrotask和micromask才恍然大悟... 先来一道面试题助助 ...

  3. mariadb sequence

    MariaDB 10.3 正式版推出后,有了像 Oracle.PostgreSQL 里的序列特性. 同时表字段AUTO_INCREMENT原特性还保持,但是sequence特性在某些特定情境还是很有用 ...

  4. vue 使用Slot 分发内容 学习总结。

    https://cn.vuejs.org/v2/guide/components.html#使用-Slot-分发内容    官方API地址 我对solt的理解是当组件中某一项需要单独定义,那么就应该使 ...

  5. CentOS7部署.Net Core2.0站点(中)

    继续上篇的内容,本篇来学习下nginx的配置和守护进程supervisor的使用. 一.Nginx安装及配置 (1)安装nginx sudo yum install epel-release #添加源 ...

  6. Expression Blend实例中文教程(10) - 缓冲动画快速入门Easing

    随着Rich Internet application(RIA)应用技术的发展,各个公司越来越注重于项目的用户体验性,在保证其功能完善,运行稳定的基础上,绚丽的UI和人性化的操作设计会给用户带来舒适的 ...

  7. Python基础学习总结(八)

    10.文件和异常 1.学习处理文件,让程序快速的分析大量数据,学习处理错误,避免程序在面对意外时崩溃.学习异常,异常是python创建的特殊对象,用于管理程序运行时出现的错误,提高程序的适用性,可用性 ...

  8. ComfortColor.xcs

    ComfortColor.xcs [comfort color] text=dce2e2text(bold)=dce2e2 magenta=dd3682magenta(bold)=dd3682 whi ...

  9. JDBC编程错误:Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)

    出现上面的错误是因为连接数据库的用户名不对或密码赋值不对,请对用户名和密码进行检查. 或者在程序中没有获取到正确的用户名或密码.看是否少写了用户名或密码.

  10. Springboot事务使用与回滚

    Springboot中事务的使用: 1.启动类加上@EnableTransactionManagement注解,开启事务支持(其实默认是开启的). 2.在使用事务的public(只有public支持事 ...