编程车类

2023-11-24 20:20:14 数码极客 bianji01

 

用java创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造方法

publicclassCar{

privateStringbrandName;//汽车牌子

privateintcolor;//颜色0:红1:黄2:兰...

publicCar(StringbrandName,intcolor){

this.brandName=brandName;

this.color=color;

publicvoidmove(Stringdirection,intmeters){

System.out.println("牌子为"+brandName+"的汽车向"+direction+"移动了"+meters+"米.");

publicstaticvoidmain(String[]args){

Carcar=newCar("BMW",1);

car.move("东北",100);

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Java程序设计,创建一个汽车类CarModel.java,

public class CarManufactory{private String style;private String color;private String category;public String getStyle(){return this.style;public String getColor(){return this.color;public String getCategory(){return this.category;public CarManufactory(String style,String color,String category){this.style = style;this.color = color;this.category = category;public CarManufactory(){}public class CarManufactory {private String style;private String color;private String category;public CarManufactory(String style, String color, String category) {super();this.style = style;this.color = color;this.category = category;public CarManufactory() {super();// TODO Auto-generated constructor stubpublic String getStyle() {return style;public String getColor() {return color;public String getCategory() {return category;public class CarModel{private String style;private String color;private String category;public CarModel(String style, String color, String category) {this.style = style;this.color = color;this.category = category;public CarModel(){}public String getStyle() {return style;public String getColor() {return color;public String getCategory() {return category;

声明:易趣百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系315127732@qq.com
广告位招租
横幅广告