作者:广东在线游戏网日期:
返回目录:游戏攻略
public class Car1 extends Car {
public String manufacturer;
public Car1(String color, String style, String manufacturer) {
super(color, style);
this.manufacturer = manufacturer;
}
public Car1(String color) {
this.color = color;
this.manufacturer = "上海大众e799bee5baa6e997aee7ad94e59b9ee7ad94337";
}
@Override
public String toString() {
return "Car1 [manufacturer=" + manufacturer + ", color=" + color + ", style=" + style + "]";
}
public Car1() {
super();
}
public static void main(String[] args) {
Car1 a = new Car1();
Car1 b = new Car1("黑色", "卡车", "斯泰尔");
Car1 c = new Car1("白色");
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(c.toString());
}
}
class Car {
public String color;
public String style;
public Car() {
this.color = "red";
this.style = "car";
}
public Car(String color, String style) {
this.color = color;
this.style = style;
}
public void showColor() {
System.out.println("颜色 " + this.color);
}
public void showStyle() {
System.out.println("型号 " + this.style);
}
}
以下代码运行结果
带参数构造e69da5e887aae799bee5baa6e997aee7ad94362方法,构造了:黑色轿车
-----------------------
package test.temp;
public class Car {
private String color;
private String type;
/**
* 第一个为无形参的构造方法,利用其中方法将颜色和型号设置为红色,轿车.
*/
public Car(){
this("红色", "轿车");
}
/**
* 第二个为带参构造方法
* @param color 颜色
* @param type 车型
*/
public Car(String color, String type){
this.color = color;
this.type = type;
}
/**
* 利用构造方法将颜色和型号设置为黑色,轿车
* @param args
*/
public static void main(String[] args) {
Car car = new Car("黑色", "轿车");
System.out.println("带参数构造方法,构造了:"+car.getColor()+car.getType());
}
/**
* 另外该类创建两个方法,分别用来显示颜色和型号
* @return 返回颜色
*/
public String getColor() {
return color;
}
/**
* 另外该类创建两个方法,分别用来显示颜色和型号
* @return 返回车型
*/
public String getType() {
return type;
}
}
如果接口的实现类有很多,那么你可以像下面这百样,把所有实现类的类名在一个地方度定义。可以是一个property文件, 可以是数据库,也可以直接写内在代码里。也可以用工厂模式,也可以用代理容,spring等等,方法很多,就不一一列举了。。。
public class Car {
public static void main(String[] args) throws Exception{
List<String> classList = new ArrayList<String>();
classList.add("Car1");
classList.add("Car2");
for(String className : classList) {
Carinterface car = (Carinterface) Class.forName(className).newInstance();
System.out.println(car.getName() + " : " + car.getPrice());
}
}
}
class CCar
{
public:
CCar();
CCar(int nColor = NULL, char *str = NULL);
~e69da5e887aae799bee5baa6e79fa5e98193334CCar();
void SetColor(int nColor);
int GetColor(){return m_Color};
void SetStyle(char *str);
char *GetStyle(){return m_Style};
protected:
int m_Color;
char *m_Style;
};
CCar::CCar()
{
m_Color=0;
m_Style=new char[10];
sprintf(m_Style,"Normal");
}
CCar::CCar(int nColor, char *str)
{
m_Color=nColor;
char *p=str;
while(p!='\0') p++;
delete m_Style;
m_Style=new char[p-str];
sprintf(m_Style,"%s",str);
}
CCar::~CCar()
{
delete m_Style;
}
void CCar::SetColor(int nColor)
{
m_Color=nColor;
}
void CCar::SetStyle(char *str)
{
char *p=str;
while(p!='\0') p++;
delete m_Style;
m_Style=new char[p-str];
sprintf(m_Style,"%s",str);
}