java基础 面向对象1
2015-09-01 23:13
417 查看
//类
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.成员方法 以“.”的形式在主函数中调用
相关文章推荐
- 11章 持有对象
- Spring笔记――5.深入理解容器中的Bean
- java IO 刘 File 基础
- java 的接口使用
- java9-1
- Spring学习(十一)spring mvc上传下载
- JVM自动内存管理
- 【Java】代处理?代理模式 - 静态代理,动态代理
- mac ox s系统下的jdk版本切换
- Spring MVC中的一些小的知识点
- 自学Java系列 笔记5 类和对象
- 自学Java系列 笔记5 整理note
- 自学Java系列 笔记5 Java学习之匿名对象
- 自学Java系列 笔记5 Java学习之Java中增强型for循环的样例和解释
- 自学Java系列 笔记5 Java学习之java Map及Map.Entry详解
- 自学Java系列 笔记5 Java学习之Iterator(迭代器)的一般用法
- 自学Java系列 笔记5 Java学习之HashMap和Hashtable的区别
- 自学Java系列 笔记4 线程安全
- [笔记][Java7并发编程实战手册]4.9-4.10在执行器中控制任务的完成和取消任务FutureTask
- 自学Java系列 笔记4 多线程 2