//繼承抽象類Car并實現(xiàn)接口Gasoline
成都創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計制作的專業(yè)網(wǎng)站制作公司,擁有經(jīng)驗豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨特的設(shè)計風(fēng)格。自公司成立以來曾獨立設(shè)計制作的站點成百上千家。
class MyCar extends Car implements Gasoline{
//定義一個變量模擬當(dāng)前檔位,如1,2,3,4,5,
public int nowShift;
//無參構(gòu)造方法,默認(rèn)設(shè)置相關(guān)屬性
public MyCar(){
this.color=Color.red;
this.gearNum=5;
this.tiretype="BridgeStone185ST";
this.engine=(float)1598.5;
}
//自己創(chuàng)建車時指定相關(guān)屬性
public MyCar(Color c,int gearNum,String tiretype,float engine){
this.gearNum=gearNum;
this.color=c;
this.tiretype=tiretype;
this.engine=engine;
}
public void shiftgear(){
//簡單模擬循環(huán)檔,每次調(diào)用時檔位加1,加滿后歸零
nowShift++;
if(nowShift=5)
nowShift=0;
}
public void brake(){
nowShift=0;
System.out.println("正在剎車...");
}
public void aircon(){
System.out.println("冷氣已經(jīng)打開");
}
public void headlight(){
System.out.println("大燈打開...");
}
public void refuel(){
System.out.println("轎車燃料為:"+FUEL);
}
public void equipment(){
System.out.println("轎車顏色:"+color+" "+"排擋數(shù):"+gearNum+"\n"+"輪胎型號:"+tiretype+" "+"尾氣排量:"+engine+" "+"轎車燃料:"+FUEL);
}
public static void main(String[]a){
new MyCar().equipment();
}
}
main()方法里只測試了自定義的equitment()方法,其他的和他一樣調(diào)用,如果你需要的話、希望對你有幫助
public abstract class Vehicle {
protected int Wheel;
protected int Weight;
public Vehicle(int wheel,int weight){
}
public abstract void Speak();
}
public interface Moveable {
double MoveSpeed();
}
public class Car extends Vehicle implements Moveable{
public Car(){
super(4,1000);
super.Weight = 1000;
super.Wheel = 4;
}
public void Speak() {
System.out.println("Car speak");
}
public double MoveSpeed() {
return 250;
}
}
public class Truck extends Vehicle implements Moveable{
public Truck() {
super(4,2000);
super.Weight = 2000;
super.Wheel = 4;
}
public void Speak() {
System.out.println("Truck speak");
}
public double MoveSpeed() {
return 450;
}
}
public class TestVehicle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle car = new Car();
System.out.println("Wheel number: "+car.Wheel+" Weight is: "+car.Weight);
car.Speak();
Car c = (Car)car;
System.out.println("Top speed: "+c.MoveSpeed());
Vehicle truck = new Truck();
System.out.println("Wheel number: "+truck.Wheel+" Weight is: "+truck.Weight);
truck.Speak();
Truck t = (Truck)truck;
System.out.println("Top speed: "+t.MoveSpeed());
}
}
如下:
public class Car {
private String brandName ; // 汽車牌子
private int color; // 顏色 0:紅 1:黃 2:蘭 ...
public Car( String brandName, int color ){
this.brandName = brandName;
this.color = color;
}
public void move( String direction, int meters ){
System.out.println("牌子為"+ brandName + "的汽車向"+ direction + "移動了"+meters+"米.");
}
public static void main(String[] args){
Car car = new Car( "BMW", 1 );
car.move("東北", 100 );
}
介紹
Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。
Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
public abstract class Vehicle{
public String NoOfWheels();
}
public class Car extends Vehicle{
public String NoOfWheels(){
return "四輪車";
}
}
public class Motorbike extends Vehicle{
public String NoOfWheels(){
return "雙輪車";
}
}
public class Test{
public static void main(String[] args){
Vehicle car =new Car();
Vehicle motorbike=new Motorbike();
System.out.println(car.NoOfWheels());
System.out.println(motorbike.NoOfWheels());
}
}
//file1
package?com.car;
public?abstract?class?Car?{
private?String?name;
private?String?color;
public?abstract?String?getType();
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getColor()?{
return?color;
}
public?void?setColor(String?color)?{
this.color?=?color;
}?
}
//file2
package?com.car;
public?class?BMWCar?extends?Car?{
private?String?type;
public?BMWCar(String?name,?String?color)?{
setName(name);
setColor(color);
this.type="BMW";
}
@Override
public?String?getType()?{
return?type;
}
public?static?void?main(String[]?args)?{
Car?car?=?new?BMWCar("baoma",?"white");
System.out.println(car.getType());
}
}
package test;
/**
*
* @author JinnL
*父類抽象類
*/
public abstract class Car {
//轉(zhuǎn)彎
abstract void turn();
//啟動
abstract void start();
void what(){
System.out.println("this is "+this.getClass().getSimpleName());
}
public static void main(String[] args) {
/**
* 方法入口
*/
Car[] cars ={new Bicycle(),new Automobile(),new GasAutomobile(),new DieselAutomobile()};
for (Car car : cars) {
car.start();
}
}
}
class Bicycle extends Car{
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName());
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName());
}
void what(){
}
}
class Automobile extends Car{
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName());
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName());
}
}
class GasAutomobile extends Automobile{
//重寫start turn
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName());
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName());
}
}
class DieselAutomobile extends Automobile{
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName());
}
void what(){
System.out.println("this is "+this.getClass().getSimpleName());
}
}