static final 常量
- 必须声明同时初始化,不可被改变
- 通过类名.来访问
- 声明的常量要大写
- 在编译时直接替换成常量值
- 俩个修饰符位置可互换
抽象方法
- 由abstract修饰
- 只有方法的定义,没有方法体—小括号后直接是分号
抽象类
- 由abstract修饰
有抽象方法的类必须用abstract修饰,若类中无抽象方法也可声明为抽象类但无意义 - 抽象类不能被实例化
- 抽象类需要被继承,单独存在无意义
1)继承抽象类的子类,需要重写抽象方法
2)继承抽象类的子类也可声明为抽象类—很少用 - 抽象类的意义
1)封装了一个共有的属性和行为,避免代码重复
2)为所有子类提供一个公共的类型
3)抽象类中可以包含抽象方法—为所有子类提供了一个方法 - final和abstract不能同时修饰一个类
接口
- 接口是一个标准,规范,只有遵守它才能干某一些事
- 接口通过interface来定义
- 接口只能包含常量和抽象方法
- 接口不能被实例化
- 接口是需要被实现/继承的,实现类/子类中必须重写接口中的抽象方法
- 接口可以继承接口
- 接口通过implements来实现,一个类可以实现多个接口
- 若又继承又实现,必须先继承再实现
练习
1.
//接口的演示
public class InterfaceDemo {
public static void main(String[] args) {
/**
* 1.声明接口Inter1,包含常量PI和抽象方法a--写完整
* 2.声明接口Inter2,包含a()和b()
* 声明类Aoo,实现Inter2
* 3.声明接口Inter3,包含c()
* 声明类Boo,实现Inter2和Inter3
* 4.声明抽象类Coo,包含抽象方法d()
* 声明类Doo,继承Coo并实现Inter2和Inter3
* 5.声明接口Inter4,继承Inter3,包含e()
* 声明类Eoo,实现Inter4
* 6.在main()方法中:
* Inter4 o1 = new Inter4()-------???
* Inter3 o3 = new Eoo();---------???
*/
//Inter4 o1=new Inter4(); //接口不能被实例化
Inter3 o3 = new Eoo(); //向上造型
}
}
//接口的定义
interface Inter1{
public static final double PI=3.1415926; //public static final---如果不写则默认提供
public abstract void a(); //public abstract---如果不写则默认提供
}
interface Inter2{
public abstract void a();
public abstract void b();
}
//接口的实现---重写所有抽象方法
class Aoo implements Inter2{
public void a(){}
public void b(){}
}
interface Inter3{
public abstract void c();
}
//一个类可以实现多个接口
class Boo implements Inter2,Inter3{
public void a(){}
public void b(){}
public void c(){}
}
//包含抽象方法的抽象类
abstract class Coo{
public abstract void d();
}
//先继承后实现
class Doo extends Coo implements Inter2,Inter3{
public void d(){}
public void a(){}
public void b(){}
public void c(){}
}
interface Inter4 extends Inter3{
public abstract void e();
}
class Eoo implements Inter4{
public void e(){}
public void c(){}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
2.
//求多种图形最大面积
public class MaxArea {
public static void main(String[] args) {
Shape[] sh=new Shape[]{new Circle(2),new Circle(3),new Squre(3),new Squre(4)};
maxArea(sh);
}
public static void maxArea(Shape[] sh){
double max=sh[0].area();
int index=0;
for(int i=1;i<sh.length;i++){
if(max<sh[i].area()){
max=sh[i].area();
index=i;
}
}
System.out.println(index+"图形是最大面积,面积是"+max);
}
}
abstract class Shape{ //图形---抽象类
double c; //周长
public abstract double area();
}
class Circle extends Shape{ //圆形
public Circle(double c){
super.c=c;
}
public double area(){ //面积
return 0.0796*c*c;
}
}
class Squre extends Shape{ //方形
public Squre(double c){
super.c=c;
}
public double area(){
return 0.0625*c*c;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
3.
//银行卡系统(实现银联接口)---工行卡特有在线支付功能,农行卡特有支付电话费功能
import java.util.Scanner;
public class UnionPayTest {
public static void main(String[] args) {
/**
*取钱方法(drawMoney(double number))
*查询余额方法(getBalance())
*检验密码方法(checkPwd(String input))
*在线支付方法(payOnline(double num))
*支付电话费方法(payTel(String phoneNum,double calls))
*/
Scanner sc=new Scanner(System.in);
ICBCImpl icbc=new ICBCImpl(3000,"258");
System.out.print("请输入密码:");
String input=sc.next();
if(icbc.checkPwd(input)){
System.out.print("请输入金额:");
double number=sc.nextInt();
if(icbc.drawMoney(number)){
System.out.println("取钱成功,卡内余额"+icbc.genBalance());
}else{
System.out.println("余额不足!");
}
}else{
System.out.println("密码错误!");
}
}
}
//银联接口
interface UnionPay{
public boolean drawMoney(double number);
public boolean checkPwd(String input);
public double genBalance();
}
//工行卡接口
interface ICBC extends UnionPay{
public void payOnline(double num);
}
//农行卡接口
interface ABC extends UnionPay{
public boolean payTel(String phoneNum,double calls);
}
//实现工行卡接口
class ICBCImpl implements ICBC{
private double money; //余额
private String pwd; //密码
public ICBCImpl(double money,String pwd){
this.money=money;
this.pwd=pwd;
}
public void payOnline(double num){
if(num>money){
money-=num;
}
}
public boolean drawMoney(double number){
if(number<=money){
money-=number;
return true;
}
return false;
}
public boolean checkPwd(String input){
if(pwd.equals(input)){
return true;
}
return false;
}
public double genBalance(){
return money;
}
}
//实现农行卡接口
class ABCImpl implements ABC{
private double money;
private String pwd;
public ABCImpl(double money,String pwd){
this.money=money;
this.pwd=pwd;
}
public boolean payTel(String phoneNum,double calls){
if(phoneNum.length()==11&&calls<money){
return true;
}
return false;
}
public boolean drawMoney(double number){
if(number-money>=-2000){
money-=number;
return true;
}
return false;
}
public boolean checkPwd(String input){
if(input.equals(pwd)){
return true;
}
return false;
}
public double genBalance(){
return money;
}
}