面向对象
1,发展:机器语言(0101) 汇编语言(操作系统) 高级语言(c) 面向对象(java)
2,万事万物皆对象
3,一类相同属性的对象集合(抽象)–类
public class Car {
/**
* @author 张金晓
* @time 2019 03 11
* @content class
*/
public String color;
public String brand;
public String number;
public int price;
}
public class Test {
/**
* @author 张金晓
* @time 2019 03 11
* @content class
*/
public static void main(String[] args) {
Car c = new Car();
c.color = "白";
c.brand = "大众";
c.number = "鲁.J8888";
c.price = 208000;
System.out.println(c.color+" "+c.brand+" "+c.number+" "+c.price);
}
}
结果:白 大众 鲁.J8888 208000
4,类(类名首字母大写),对象
5,属性—是对类和对象的静态描述
public class Box {
public int lon;
public int wigth;
public int highth;
public void area(int lon,int wight,int highth){
int a = (lon*wight+lon*highth+wight*highth)*2;
System.out.println("面积是"+a);
}
public void volume(int lon,int wight,int highth){
int v = lon*wight*highth;
System.out.println("体积是"+v);
}
}
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box();
b1.area(2, 4, 5);
b1.volume(2, 4, 5);
b2.area(3, 6, 7);
b2.volume(3, 6, 7);
}
}
结果:
面积是76
体积是40
面积是162
体积是126
6,方法------动态描述
public 返回值类型 方法名(参数列表){
方法体
}
返回值类型 return void
方法名:标识符(驼峰)
参数列表:p.eatFood(“刀削面”); 实参 String food --形参
7,创建对象Person p = new Person();
8,调用 对象名.属性[方法]
9,内存模型 new :开辟空间
10,方法的重载:方便记忆
要求:方法名相同 参数必须不同(个数,类型,顺序)
11,构造函数:构造方法 :
1)语法结构:public + 类名(参数列表){方法体}
2)作用:创建对象 初始化属性
3)当没有显示声明构造函数时,系统会默认提供一个空的构造函数(缺省构造函数)
4)当显示声明构造函数,系统则不提供
5)当形参名和属性名相同。用this区分
12,this:即将出现的对象
this():调用当前类构造函数,放在第一行
this.:调用属性或者方法
普通方法不能调用构造方法
public class WuMingFen {
public String theMa;
public int quantity;
public boolean likeSoup;
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
super();
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
public WuMingFen(String theMa, int quantity) {
super();
this.theMa = theMa;
this.quantity = quantity;
}
public WuMingFen() {
this.theMa = "酸辣面";
this.quantity = 2;
this.likeSoup = true;
}
public void check(){
if(this.theMa==theMa&&this.quantity == quantity&&this.likeSoup == likeSoup){
System.out.println(theMa+quantity+likeSoup+"符合要求");
}else{
System.out.println("不符合要求");
}
}
}
public class Test1 {
/**
* @param args
*/
public static void main(String args[]){
WuMingFen f1 = new WuMingFen("牛肉",3,true);
WuMingFen f2 = new WuMingFen("牛肉",2);
WuMingFen f3 = new WuMingFen();
f1.check();
f2.check();
f3.check();
}
}
牛肉3true符合要求
牛肉2false符合要求
酸辣面2true符合要求