//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. //second
  11. 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. @Override
  34. 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. @Override
  43. 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. }

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. Intellij IDEA自定义类模板和方法模板

    以Intellij IDEA 2017.3.5为例 定义类模板 依次打开File->Settings->File and Code Templates->Files, 选择class ...

  2. Java入门系列-16-继承

    这一篇文章教给新手学会使用继承,及理解继承的概念.掌握访问修饰符.掌握 final 关键字的用法. 继承 为什么要使用继承 首先我们先看一下这两个类: public class Teacher { p ...

  3. bzoj 5252: [2018多省省队联测]林克卡特树

    Description 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的迷你挑战. 游戏中有一个叫做& ...

  4. 架构实战项目心得(四)(补):Maven settings.xml的所有标签详解

    文章内容较长,各位看客可以根据自己需要CTRL+F 直接定位到自己需要了解的地方哦~ <?xmlversion="1.0" encoding="UTF-8" ...

  5. 一:HttpClient知识整理

    一:httpclient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支 ...

  6. 四、jdbctemplate使用

    这里使用mysql数据库,省略数据库创建过程 1.添加依赖 <!--jdbc--> <dependency> <groupId>org.springframewor ...

  7. spring_boot启动报错

    配置好pom文件后,在controller加注解,如下: 运行后报错!!! 发现配置加的是多此一举,修改为下边的,运行OK

  8. 6、springboot之根目录设置

    访问的时候用

  9. IOC和DI到底是什么?

     在学习Spring框架的时候,我们总是能见到IOC这个单词,也时常听到DI这个词,那么他们分别是什么意思呢?接下来就讲讲个人对于这两个概念的理解  一.IOC和DI概念 IOC(控制反转):全称为: ...

  10. img底部空白以及多余的空白文本节点解决方案

    1:img底部有空白的问题 img的css属性display的默认值是inline,这样会导致img的vertical-align的默认值是 baseline; baseline又不是bottom,只 ...