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中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
随机推荐
- Intellij IDEA自定义类模板和方法模板
以Intellij IDEA 2017.3.5为例 定义类模板 依次打开File->Settings->File and Code Templates->Files, 选择class ...
- Java入门系列-16-继承
这一篇文章教给新手学会使用继承,及理解继承的概念.掌握访问修饰符.掌握 final 关键字的用法. 继承 为什么要使用继承 首先我们先看一下这两个类: public class Teacher { p ...
- bzoj 5252: [2018多省省队联测]林克卡特树
Description 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的迷你挑战. 游戏中有一个叫做& ...
- 架构实战项目心得(四)(补):Maven settings.xml的所有标签详解
文章内容较长,各位看客可以根据自己需要CTRL+F 直接定位到自己需要了解的地方哦~ <?xmlversion="1.0" encoding="UTF-8" ...
- 一:HttpClient知识整理
一:httpclient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支 ...
- 四、jdbctemplate使用
这里使用mysql数据库,省略数据库创建过程 1.添加依赖 <!--jdbc--> <dependency> <groupId>org.springframewor ...
- spring_boot启动报错
配置好pom文件后,在controller加注解,如下: 运行后报错!!! 发现配置加的是多此一举,修改为下边的,运行OK
- 6、springboot之根目录设置
访问的时候用
- IOC和DI到底是什么?
在学习Spring框架的时候,我们总是能见到IOC这个单词,也时常听到DI这个词,那么他们分别是什么意思呢?接下来就讲讲个人对于这两个概念的理解 一.IOC和DI概念 IOC(控制反转):全称为: ...
- img底部空白以及多余的空白文本节点解决方案
1:img底部有空白的问题 img的css属性display的默认值是inline,这样会导致img的vertical-align的默认值是 baseline; baseline又不是bottom,只 ...