本文从机器语言到面向对象的发展历程出发,深入浅出地讲解了面向对象的基本概念,包括类和对象的定义、属性和方法的使用、构造函数的作用及应用实例。

面向对象

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符合要求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值