class Car
成都創新互聯專業成都做網站、成都網站制作,集網站策劃、網站設計、網站制作于一體,網站seo、網站優化、網站營銷、軟文平臺等專業人才根據搜索規律編程設計,讓網站在運行后,在搜索中有好的表現,專業設計制作為您帶來效益的網站!讓網站建設為您創造效益。
{
// 車輛屬性
private String brand; // 品牌
private double engineDisplacement;// 排氣量
private double speed;// 當前速度
private boolean status;// 啟動狀態
private double maxSpeed;// 最大速度
public double getSpeed () {
return this.speed;
}
public Car(String brand, double engineDisplacement, double maxSpeed) {
this.brand = brand;
this.engineDisplacement = engineDisplacement;
this.maxSpeed = maxSpeed;
// 其他屬性也要有初始值,不然執行出錯。
this.speed = 0;
this.status = false;
}
/** 啟動 */
public void start() {
this.status = true;
printNowStatus ();
}
/** 關閉(熄火) */
public void stop() {
// 只在速度為0時關閉(貌似樓上兩位沒仔細看題…)
if (this.speed == 0)
{
this.status = false;
}
printNowStatus ();
}
/** 加速 */
public void speedUp() {
// 只在啟動時可以加速
if (this.status)
{
this.speed = this.speed + 20;
if (this.speed this.maxSpeed)
{
this.speed = this.maxSpeed;
}
}
printNowStatus ();
}
/** 減速 */
public void slowDown() {
// 只在啟動時可以減速
if (this.status)
{
this.speed = this.speed - 10;
if (this.speed 0)
{
this.speed = 0;
}
}
printNowStatus ();
}
/** 狀態打印,在每次啟動,加減速,關閉時調用 */
private void printNowStatus () {
System.out.println("轎車【" + this.brand + "】現在的啟動狀態是:" + this.status + "速度是:" + this.speed +"。");
}
}
public class TestCar
{
public static void main(String[] args)
{
Car myCar = new Car ("紅旗", 2, 120);
//啟動
myCar.start();
// 循環加速到120
while (myCar.getSpeed() 120)
{
myCar.speedUp();
}
//循環減速
while (myCar.getSpeed() 0)
{
myCar.slowDown();
}
//關閉
myCar.stop();
}
}
/* 直接拿文本寫的,我用的電腦沒裝jdk,樓主自己到Java開發環境下調試,應該沒什么問題… */
import java.lang.*;
class Car{
public void safe(){
System.out.println("an quan xi shu gao !");
}
}
class Benz extends Car{
public void safe(){
System.out.println("che hao !");
}
}
class Santana extends Car{
public void safe(){
System.out.println("che hen bu hao hao !");
}
}
public class Test{
public static void main(String[]args){
Car s = new Car();
s.safe();
Benz s1 = new Benz();
s1.safe();
Santana s2 = new Santana();
s2.safe();
Car s3 = new Benz();
s3.safe();
Car s4 = new Santana();
s4.safe();
}
}
這是一個 命名為Test.java
public class Car{
double price;
String name;
int id;
//3個重載的構造方法(name)(id,name)(id,name,price)
public Car(String name){
this(789,name);
}
public Car(int id,String name){
this(id,name,100000);
}
protected Car(int id,String name,double price){
this.price = price;
this.name = name;
this.id = id;
}
//3個重載的普通方法drive(int) drive(String) drive(int ,String)
public void drive(int i){
System.out.println("i = " + i);
System.out.println(price + "\n" + name + "\n" + id);
}
protected String drive(String s){
return "s is: " + s;
}
int drive(int i,String s){
return 2007;
}
}
class Test{
public static void main(String[]args){
Car c1 = new Car(777,"Santana");
c1.drive(18);
}
}
這個命名為Car.java
希望有你要的!
public class Car {
private String color;//顏色
private int door;//車門數量
private float speed;//車速
public Car(){
this.color = "紅色";
this.door = 3;
this.speed = 110;
}
public Car(String color, int door, float speed) {
this.color = color;
this.door = door;
this.speed = speed;
}
public void start(){
//汽車啟動。輸出汽車已啟動,并輸出汽車的各個屬性
System.out.println("汽車已啟動,汽車顏色為"+color+",車門數為"+door+",車速為"+speed);
}
public void speedUp(float speed){
//加速
System.out.println("汽車加速到"+speed+"km/h");
}
public void shutDown(float speed){
//減速
System.out.println("汽車減速到"+speed+"km/h");
}
public void brake(){
//剎車
System.out.println("已剎車");
}
}
public class Test {
public static void main(String[] args){
Car car = new Car();
car.start();
car.speedUp(100);
car.shutDown(60);
car.brake();
Car car1 = new Car("白色",4,20.2F);
car1.start();
car1.speedUp(100);
car1.shutDown(60);
car1.brake();
}
}
運行結果
第一題:
Car.java
public?class?Car?{
public?String?num;
public?float?price;
public?Car(String?num,float?price){
this.num?=?num;
this.price=price;
System.out.println("車牌號為:"+num+",價格為:"+price);
}
}
測試類:
public?class?CarTest?{
public?static?void?main(String[]?args)?{
Car?car?=?new?Car("京P34E68",?100000.23f);
}
}
輸出為:車牌號為:京P34E68,價格為:100000.23
第二題:
Student.java
public?class?Student?{
private?String?num;
private?String?name;
private?int?age;
private?String?sex;
public?String?getNum()?{
return?num;
}
public?void?setNum(String?num)?{
this.num?=?num;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
public?String?getSex()?{
return?sex;
}
public?void?setSex(String?sex)?{
this.sex?=?sex;
}
public?String?tell()?{
return?"Student?[num="?+?num?+?",?name="?+?name?+?",?age="?+?age+?",?sex="?+?sex?+?"]";
}
public?static?void?main(String[]?args)?{
Student?student?=?new?Student();
student.setAge(18);
student.setNum("001");
student.setName("COLD");
student.setSex("男");
System.out.println(student.tell());
}
}
輸出:Student [num=001, name=COLD, age=18, sex=男]
希望能幫到你,望采納
public?class?Car?{
private?int?num;//編號
private?String?name;//型號
private?double?price;//單價
/**
?*?無參構造
?*/
public?Car(){
super();
}
/**
?*?有參構造
?*?@param?num
?*?@param?name
?*?@param?price
?*/
public?Car(int?num,?String?name,?double?price)?{
super();
this.num?=?num;
this.name?=?name;
this.price?=?price;
}
public?int?getNum()?{
return?num;
}
public?void?setNum(int?num)?{
this.num?=?num;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?double?getPrice()?{
return?price;
}
public?void?setPrice(double?price)?{
this.price?=?price;
}
public?String?inforShow()?{
return?"Car?[num="?+?num?+?",?name="?+?name?+?",?price="?+?price?+?"]";
}
}
public?class?PriCar?extends?Car{
private?int?PersonNum;//最大載客量
public?PriCar(int?personNum)?{
super();
PersonNum?=?personNum;
}
public?PriCar()?{
super();
}
public?int?getPersonNum()?{
return?PersonNum;
}
public?void?setPersonNum(int?personNum)?{
PersonNum?=?personNum;
}
@Override
public?String?inforShow()?{
return?"PriCar?[PersonNum="?+?PersonNum?+?"]";
}
}
public?class?VanCar?extends?Car?{
private?double?weight;//最大載重
public?VanCar(double?weight)?{
super();
this.weight?=?weight;
}
public?VanCar()?{
super();
}
@Override
public?String?inforShow()?{
return?"PriCar??[num="?+?super.getNum()?+?",?name="?+?super.getName()?+?",?price="?+?super.getPrice()?+",weight="?+?weight?+?"]";
}
}
測試類不想寫了??應該可以自己寫出來了吧
分享名稱:汽車會員JAVA代碼,汽車會員java代碼大全
瀏覽路徑:http://vcdvsql.cn/article44/heceee.html
成都網站建設公司_創新互聯,為您提供App設計、營銷型網站建設、網站維護、移動網站建設、ChatGPT、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯