java基础 面向对象1

//类
public class Car1 {
//属性
String brand;
String color;
int length;
int width;
double emissions;
int price;
//构造方法
public Car1(){//无参构造

}

public Car1(String brand, String color, int length, int width,
double emissions, int price) {
super();
this.brand = brand;
this.color = color;
this.length = length;
this.width = width;
this.emissions = emissions;
this.price = price;
}


// public Car1(String brand,String color,int length,int width,double emissions,int price){
// this.brand = brand;
// this.color = color;
// this.length = length;
// this.width = width;
// this.emissions = emissions;
// this.price = price;
// }//有参构造ss
//方法
public void start(){
System.out.println(this.brand+"出发");
}
public void stop(){
System.out.println("停");
}
public String toString(){
return this.brand+","+this.color+","+this.emissions+","+this.length+","+this.price+","+this.width;
}
}




public class TestCar1 {
public static void main(String[] args) {
//创建对象

Car1 mycar1 = new Car1();
//new Car后会在内存中分配Car类型所需属性的存储空间
//无参构造内存中的值为默认值
//变量名mycar1放到了栈中,就是Car类型的对象
//mycar1中的属性在堆中
//mycar1存放的是堆中的地址
System.out.println(mycar1);

Car1 mycar2 = new Car1("xx","白色",4555,1788,2.0f,600000);
mycar2.start();
System.out.println(mycar2);

}
}




总结:

1.类

2.构造方法 有参 无参

3.成员方法 以“.”的形式在主函数中调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值