汽车抽象类java代码 用java编写汽车类
java中怎么具体使用抽象类和接口???
//继承抽象类Car并实现接口Gasoline
成都创新互联是一家从事企业网站建设、成都网站建设、成都网站制作、行业门户网站建设、网页设计制作的专业网站制作公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点成百上千家。
class MyCar extends Car implements Gasoline{
//定义一个变量模拟当前档位,如1,2,3,4,5,
public int nowShift;
//无参构造方法,默认设置相关属性
public MyCar(){
this.color=Color.red;
this.gearNum=5;
this.tiretype="BridgeStone185ST";
this.engine=(float)1598.5;
}
//自己创建车时指定相关属性
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(){
//简单模拟循环档,每次调用时档位加1,加满后归零
nowShift++;
if(nowShift=5)
nowShift=0;
}
public void brake(){
nowShift=0;
System.out.println("正在刹车...");
}
public void aircon(){
System.out.println("冷气已经打开");
}
public void headlight(){
System.out.println("大灯打开...");
}
public void refuel(){
System.out.println("轿车燃料为:"+FUEL);
}
public void equipment(){
System.out.println("轿车颜色:"+color+" "+"排挡数:"+gearNum+"\n"+"轮胎型号:"+tiretype+" "+"尾气排量:"+engine+" "+"轿车燃料:"+FUEL);
}
public static void main(String[]a){
new MyCar().equipment();
}
}
main()方法里只测试了自定义的equitment()方法,其他的和他一样调用,如果你需要的话、希望对你有帮助
java代码题,要能编译得出结果啊!
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());
}
}
用java创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造方法
如下:
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是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
java编程 抽象类: 创建一个Vehicle类并将它声明为抽象类
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());
}
}
Java考试编程,小汽车定义成抽象类,成员方法定义成抽象方法,重写实现抽象方法,怎么用子类实现抽象
//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());
}
}
java程序,抽象类实现,求具体代码
package test;
/**
*
* @author JinnL
*父类抽象类
*/
public abstract class Car {
//转弯
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());
}
}
新闻标题:汽车抽象类java代码 用java编写汽车类
URL分享:http://cdiso.cn/article/hjiepo.html