1、面向对象的特点主要为封装性,继承性和多态性。
2、发展:机器语言(0101)、汇编语言(操作系统)、高级语言(c语言,面向过程)、面向对象(java)
3、万事万物皆对象
人法地,地法天,天法道,道法自然
例如: 床前明月光,疑是地上霜。举头望明月,低头思故乡。
对象分别有:床、明月光、霜、头、明月、故乡
4、类:一类相同属性的对象集合(抽象)
5、属性-是对类和对象的静态描述
方法-是对类和对象的动态描述
public 返回值类型 方法名(参数列表){
方法体
}
返回值类型 return void
方法名:标识符(驼峰)
参数列表:p.eatFood(“刀削面”); 实参 String food --形参
例1:定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;
public class Player {
public double height;
public double weight;
public String name;
public Player(double h,double w,String n){
this.height = h;
this.weight = w;
this.name = n;
}
}
public static void main(String[] args) {
Player p = new Player(2.15,60.0,"姚明");
Player pp = new Player(2.25,65.0,"科比");
System.out.println("身高: "+p.height+" 体重: "+p.weight+" 体重: "+p.name);
System.out.println("身高: "+pp.height+" 体重: "+pp.weight+" 体重: "+pp.name);}
}
例2:设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积和表面积
public class Box {
public double length;
public double width;
public double high;
public double size(){
return (length*width+width*high+high*length)*2;
}
public double Volume(){
return length*width*high;
}
}
public static void main(String[] args) {
Box b = new Box();
b.high = 12.0;
b.length = 13.0;
b.width = 1.0;
System.out.println("表面积为 "+b.size());
System.out.println("体积为"+b.Volume());
}
6、创建对象Person p = new Person();
调用 对象名.属性[方法]
如:v.speedUp();
内存模型 new :开辟空间
7、方法的重载:方便记忆
要求:方法名相同 参数必须不同(个数,类型,顺序)
例如:p
ublic double jiaFa(int a,double b,double c){
return a+b+c;
}
public double jiaFa(double a,int b,double c){
return a+b+c;
}
8、构造函数:构造方法 :
1)语法结构:public + 类名(参数列表){方法体}
2)作用:创建对象 (或初始化属性)
3)当没有显示声明构造函数时,系统会默认提供一个空的构造函数(缺省构造函数)
4)当显示声明构造函数,系统则不提供
5)当形参名和属性名相同。用this区分
例3打印int,char, float,double,String ,boolean这些数据类型作为类属性时的默认值
public class Zifu {
public float f;
public int i;
public char c;
public double d;
public boolean b;
public string s;
public Zifu(){}}
public static void main(String[] args) {
Zifu z = new Zifu();
System.out.println(z.b+" "+z.c+" "+z.d+" "+z.f+" "+z.i+z.s);
}
例4、请定义一个交通工具(Vehicle)的类,其中有:
属性:速度(speed),体积(size)等等
方法:移动(move(int s)),设置速度(setSpeed(int speed)),加速speedUp(),减速speedDown()等等.
最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速度进行改变。调用 move方法输出移动距离
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.size = 10.0;
v.speed = 20;
v.move(521);
v.setSpeed(21);
v.speedUp();
v.speedUp();
v.speedDown();
System.out.println(v.speed);
}
public class Vehicle {
public double speed;
public double size;
public void move(int s){
System.out.println("移动距离"+s);
}
public void setSpeed(int speed){
this.speed = speed;
}
public void speedUp(){
this.speed+=10;
}
public void speedDown(){
this.speed-=10;
}
}
9、this:即将出现的对象
this():调用当前类构造函数,放在第一行
this.:调用属性或者方法
例5、定义一个人类(Person),该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
public class Person {
private String name;
private int age;
public void Person(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display(){
System.out.println("我的名字是 "+this.name+" 年龄为 "+this.age);
}
}
public static void main(String[] args) {
Person p = new Person();
p.Person("哒哒", 17);
p.display();
}