oop_day06_抽象类、接口_20150814

1.static final常量:

1)必须声明同一时候初始化。不能改动,类名点来訪问

2)常量名建议全部字母都大写

3)编译器编译时会直接替换为详细的值---效率高

代码演示样例:

package oo.day06;
//static final常量
public class StaticFinalDemo {
public static void main(String[] args) {
//Aoo.NUM = 250; //编译错误。常量不能改动
//System.out.println(Aoo.NUM); //1.方法区中载入Boo.class
//2.将NUM1存储在方法区中
//3.去方法区中获取NUM1的值并输出
System.out.println(Boo.NUM1); //编译器在编译时直接被替换为详细的值。效率高
//等价于System.out.println(6);
System.out.println(Boo.NUM2);
}
}
class Boo{
public static int NUM1 = 5; //静态变量
public static final int NUM2 = 6; //常量
} class Aoo{
public static final int NUM = 5; //常量
//public static final double PI; //编译错误,必须声明同一时候初始化
}

2.抽象方法

1)由abstract修饰

2)仅仅有方法的定义。没有方法的实现(连大括号都没有)

3.抽象类:

1)由abstract修饰

2)包括抽象方法的类必须是抽象类

不包括抽象方法的类也能够声明为抽象类-----我愿意

3)抽象类不能被实例化

4)抽象类是须要被继承的,子类:

4.1)重写全部的抽象方法----建议

4.2)声明为抽象类----不建议

5)抽象类的意义:

  5.1)封装全部子类共同拥有的数据和行为。实现代码重用

      5.2)为全部子类提供了一种公共的类型(向上造型)

      5.3)包括抽象方法,为全部子类提供一个统一的入口

代码演示样例:

package oo.day06;
//求一组图形中的最大面积
public class ShapeTest {
public static void main(String[] args) {
//Shape s = new Shape(); //编译错误,抽象类不能被实例化
Shape[] shapes = new Shape[4]; //创建Shape数组对象
shapes[0] = new Circle(1); //向上造型
shapes[1] = new Circle(2);
shapes[2] = new Square(1);
shapes[3] = new Square(2);
maxArea(shapes);
}
public static void maxArea(Shape[] shapes){ //求最大面积
double max = shapes[0].area(); //最大面积
int maxIndex = 0; //最大面积索引
for(int i=1;i<shapes.length;i++){
double area = shapes[i].area();
if(area>max){
max = area;
maxIndex = i;
}
}
System.out.println("最大面积为:"+max+",所在索引为:"+maxIndex);
} } abstract class Shape{ //抽象类
protected double c; //周长
public abstract double area(); //抽象方法
}
class Circle extends Shape{
public Circle(double c){
this.c = c;
}
public double area(){ //重写抽象方法
return 0.0796*c*c;
}
}
class Square extends Shape{
public Square(double c){
this.c = c;
}
public double area(){ //重写抽象方法
return 0.0625*c*c;
}
}

4.接口:

1)是一个标准、规范-----制定方

遵守了这个标准,就能干某件事-----后期再理解

2)由interface定义

3)仅仅能包括常量和抽象方法

4)接口不能被实例化

5)接口是须要被实现(implements)的,实现类:

必须重写接口中的全部抽象方法

6)一个类能够实现多个接口。用逗号分隔

若既继承又实现时。必须先继承,后实现

7)接口之间能够继承

7.1)类和接口之间为实现(implements)

7.2)接口和接口之间为继承(extends)

7.3)类和类之间为继承(extends)

代码演示样例:

package oo.day06;
//interface演示
public class InterfaceDemo {
public static void main(String[] args) {
//Inter6 o = new Inter6(); //编译错误,接口不能被实例化
Foo o1 = new Foo();
Inter6 o2 = new Foo(); //向上造型
Inter5 o3 = new Foo(); //向上造型 }
} interface Inter5{
void a();
}
interface Inter6 extends Inter5{
void b();
}
class Foo implements Inter6{
public void b(){}
public void a(){}
} interface Inter3{
void a();
}
interface Inter4{
void b();
}
abstract class Doo{
abstract void c();
}
class Eoo extends Doo implements Inter3,Inter4{
public void a(){}
public void b(){}
void c(){}
} interface Inter2{
void a();
}
class Coo implements Inter2{
public void a(){}
} interface Inter1{
public static final int NUM=5;
public abstract void show(); double PI = 3.14159;//默认public static final
void sayHi(); //默认public abstract //public void say(){} //编译错误
//public int a; //编译错误
}

最新文章

  1. 对于前端JS、Html、CSS的大小、位置是否影响网站的相应时间
  2. 在Mac上安装Sublime Text3的插件
  3. 如何用python搞定验证码中的噪点
  4. 扩展WPF的DataGrid按方向键移动焦点
  5. java 环境配置
  6. ThinkPHP 关联模型中查询某条记录的父级(非查询子级)
  7. nrf51822裸机教程-RTC
  8. Android --账户注销
  9. 【M29】引用计数
  10. cordova在app内部指定浏览器打开链接插件:cordova-plugin-inappbrowser
  11. uva 11983 Weird Advertisement 扫描线
  12. Android 遍历sdcard中指定文件夹下的图片(jpg,jpeg,png)
  13. 20160505-hibernate入门2
  14. MySQL如何有效地创建基于 INNODB 引擎的表
  15. FileReader读取文件里文乱码问题
  16. leetcode371
  17. LightOJ1282 Leading and Trailing
  18. 用UIWebView加载本地图片和gif图
  19. 【模板】最长公共子序列(LCS)。
  20. PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press &ldquo;Fix&rdquo; to edit your project configuration.

热门文章

  1. Python3.4的Pillow库实现验证码图片
  2. Android基础TOP6_2:Gallery +Image完成画廊
  3. PHP——基本使用(一)
  4. swift VS NSObject
  5. Controller传值到前端页面的几种方式
  6. layer iframe层弹出图片
  7. 分布式数据库中CAP原理(CAP+BASE)
  8. 【16】AngularJS&#160;API
  9. 虚拟机中的CentOS7如何上网?---https://blog.csdn.net/nothing2017/article/details/61420767
  10. gif &amp; tools