JAVA 十七 抽象 常量 abstract,final
2018-02-28 11:47
239 查看
JAVA 十七 抽象,常量 abstract,final
标签(空格分隔): JAVA1.abstract抽象类
用abstract修饰的类是抽象类如果用abstract修饰的类是无法生成对象的,即无法实例化
2.abstract抽象方法
没有方法体即没有大括号{ };如果类里面包含抽象方法,那么该类必须为抽象类抽象类里面不一定有抽象方法
子类继承父类,必须重写父类抽象方法,
3.final修饰属性,方法,类
修饰的属性是常量修饰的方法为最终方法无法重写
修饰的类为最终类,无法被继承
//汽车租聘 public abstract class MotoVehicle { private int no; private String brand,color; private double mileage; public abstract void CalcRent(int days); public MotoVehicle(int no,String brand,String color,double mileage){ this.setNo(no); this.setBrand(brand); this.setColor(color); this.setMileage(mileage); } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getMileage() { return mileage; } public void setMileage(double mileage) { this.mileage = mileage; } } //CAR类 public final class Car extends MotoVehicle{ private String type; private int money; public void CalcRent(){ } public Car(int no,String brand,String color,double mileage,String type){ super(no,brand,color,mileage); this.type=type; } @Override public void CalcRent(int days) { // TODO Auto-generated method stub if(type.equals("别克GL8")){ money=days*600; } if(type.equals("宝马")){ money=days*500; } if(type.equals("别克")){ money=days*300; } } public void show(){ System.out.println("车牌号:"+super.getNo()+"品牌:"+super.getBrand()+"型号:"+type+"颜色:"+super.getColor()+"公里:"+super.getMileage()+"花费:"+money); } } //BUS类 public final class Bus extends MotoVehicle{ private String sentcount; private int money; public void CalcRent(){ } public Bus(int no,String brand,String color,double mileage,String sentcount){ super(no,brand,color,mileage); this.sentcount=sentcount; } @Override public void CalcRent(int days) { // TODO Auto-generated method stub if(sentcount.equals("金杯")){ money=days*800; } if(sentcount.equals("金龙")){ money=days*1500; } } public void show(){ System.out.println("车牌号"+super.getNo()+"品牌"+super.getBrand()+"型号"+sentcount+"颜色"+super.getColor()+"公里"+super.getMileage()+"花费"+money); } } /测试类 public class Text {< a5b0 /span> public static void main(String[] args){ System.out.println("构造方法中输入\t车牌号\t型号\t公里数\t具体型号"); Car aa=new Car(888,"轿车","绿色",2000,"别克GL8"); aa.CalcRent(8); aa.show(); Bus bb=new Bus(888,"面包车","绿色",2000,"金杯"); aa.CalcRent(10); aa.show(); } }
相关文章推荐
- java中Static&Final的使用方法,&&&Java中两个主要的抽象Abstract&&Implement
- Java-abstract(抽象)、final、static
- (转)总结下java中abstract,interface,final,static,加深理解
- JAVA修饰符的详细介绍(public protect,default, private,final,abstract)
- Javapublic、protected、private、abstract、static和final
- java中利用关键字final来声明常量
- interface,abstract class,abstract interface Java中的接口,抽象类和抽象接口的异同
- 总结下java中abstract,interface,final,static,加深理解
- 总结下java中abstract,interface,final,static,加深理解
- java第六课_继承,覆写,final关键字,抽象,接口
- JAVA基础知识(static,final,abstract)浅谈
- java之接口(interface)和抽象(abstract)关键字详解
- JAVA -- 关键字 abstract、static、final、native、synchronized
- java 自我知识总结(十四)抽象abstract
- Java final static abstract关键字介绍
- Java中得抽象-----abstract
- Java中abstract抽象类 / abstract抽象方法 和C++的对比
- java中抽象方法不能为private,final或者static
- 抽象 关键字abstract与final使用
- Java自学之路-Java基础教程-43:Java的常量和final关键字