public?class?Car?{
公司主營業(yè)務:網站制作、網站設計、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現互聯(lián)網宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出阿拉爾免費做網站回饋大家。
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?+?"]";
}
}
測試類不想寫了??應該可以自己寫出來了吧
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import com.me.util.JFrameUtil;
public class SwingCar extends JFrame {
private final int rect_Width = 80;
private final int rect_Height = 50;
private final int radius = 15;
private final int arcAngle = 30;
private Point p = new Point();
public SwingCar() {
setSize(500, 300);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrameUtil.toCenter(this);
p = new Point(getWidth() / 2, getHeight() / 2);
move();
}
@Override
public void paint(Graphics g) {
super.paint(g);
drawRect(p);
drawRolls(p);
g.drawLine(0,getHeight()/2+rect_Height/2+radius, getWidth(), getHeight()/2+rect_Height/2+radius);
}
public void drawRect(Point c) {
Graphics2D g = (Graphics2D) getGraphics();
g.setColor(Color.red);
g.drawRect((int) (c.getX() - rect_Width / 2),
(int) (c.getY() - rect_Height / 2), rect_Width, rect_Height);
g.setColor(Color.black);
g.drawLine((int) (c.getX() - rect_Width / 2)-50, (int) (c.getY() - rect_Height / 2)-rect_Height/2, (int) (c.getX() - rect_Width / 2), (int) (c.getY() - rect_Height / 2)+rect_Height/2);
g.setColor(Color.green);
g.fillOval((int) (c.getX() - rect_Width / 2)-50-2, (int) (c.getY() - rect_Height / 2)-rect_Height/2-2, 10, 10);
}
public void drawRolls(Point c) {
Graphics2D g = (Graphics2D) getGraphics();
g.setColor(Color.blue);
// first roll
g.fillOval((int) (c.getX() - rect_Width / 4 - radius), (int) (c.getY()
+ rect_Height / 2 - radius), radius * 2, radius * 2);
g.setColor(Color.green);
g.fillArc((int) (c.getX() - rect_Width / 4 - radius), (int) (c.getY()
+ rect_Height / 2 - radius), radius * 2, radius * 2,
calcAngle(c), arcAngle);
// second roll
g.setColor(Color.blue);
g.fillOval((int) (c.getX() + rect_Width / 4 - radius), (int) (c.getY()
+ rect_Height / 2 - radius), radius * 2, radius * 2);
g.setColor(Color.green);
g.fillArc((int) (c.getX() + rect_Width / 4 - radius), (int) (c.getY()
+ rect_Height / 2 - radius), radius * 2, radius * 2,
calcAngle(c), arcAngle);
}
public int calcAngle(Point c) {
Point center = new Point(getWidth() / 2, getHeight() / 2);
double distance = c.getX() - center.getX();
int angle = (int) (180 * distance / (Math.PI * radius));
return -angle;
}
public void move() {
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
p.translate(2, 0);
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
p.translate(-2, 0);
}
repaint();
}
});
}
public static void main(String[] args) {
SwingCar sc = new SwingCar();
}
}
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();
}
}
運行結果
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
希望有你要的!