一.面向对象:
1.发展:机器语言(0101) 汇编语言(操作系统) 高级语言(c) 面向对象(java)
2.万事万物皆对象
床前明月光,疑是地上霜,举头望明月,低头思故乡
对象:床 明月光 地上霜 头 明月 故乡
3.一类相同属性的对象集合(抽象)—类
例:桌子,电脑
类中固定的事物(具体)—对象
例:我的桌子,某某人的电脑
4.属性-是对类和对象的静态描述
5.方法-是对类和对象的动态描述
public 返回值类型 方法名(参数列表){
方法体
}
返回值类型 return void
方法名:标识符(驼峰)
参数列表:p.eatFood(“刀削面”); 实参 String food --形参
6.创建对象Person p = new Person();
7.调用 对象名.属性[方法]
8.内存模型 new :开辟空间
例1:定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值
public class day6Car {
public String color;
public String brand;
public String number;
public double price;
}
public class Test {
public static void main(String[] args) {
day6Car c = new day6Car();
c.color="白色";
c.brand="丰田";
c.price=18.0;
c.number="9c613";
System.out.println(c.color+" "+c.brand+" "+c.number+" "+c.price);
}
}
例2. 定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;
public class Player {
public String name;
public double height;
public double weight;
}
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Player p = new Player();//或者是Player p1 = new Player();Player p2 = new Player();
p.name = "姚明"; // p1.name = "姚明";
p.height = 2.15; // p1.height = 2.15;
p.weight = 100; // p1.weight = 100;
System.out.println(p.name+" "+p.height+" "+p.weight);
p.name = "科比"; // p2.name = "科比";
p.height = 2.10; // p2.height = 2.10;
p.weight =105; // p2.weight =105;
System.out.println(p.name+" "+p.height+" "+p.weight);
// System.out.println(p1.name+" "+p1.height+" "+p1.weight);
// System.out.println(p2.name+" "+p2.height+" "+p2.weight);
}
}
例3. 定义一个僵尸类(Zombie),属性有名字,体力值,攻击力,实例化三个僵尸类,并给属性赋值;
public class Zombie {
public String name;
public double power;
public double attack;
}
public class Test2 {
public static void main(String[] args) {
Zombie z1 = new Zombie();
Zombie z2 = new Zombie();
Zombie z3 = new Zombie();
z1.attack = 89;
z1.power =100;
z1.name ="豌豆";
z2.attack = 60;
z2.name = "土豆";
z2.power = 100;
z3.attack = 65;
z3.name = "向日葵";
z3.power = 0;
System.out.println(z1.name +" "+z1.attack+" "+z1.power );
System.out.println(z2.name +" "+z2.attack+" "+z2.power );
System.out.println(z3.name +" "+z3.attack+" "+z3.power );
}
}
例4. 打印int,char, float,double,String ,boolean这些数据类型作为类属性时的默认值
public class Default {
public int a;
public char b;
public float c;
public double d;
public String e;
public boolean f;
}
public class Test3 {
public static void main(String[] args) {
Default d = new Default();
System.out.println("int"+d.a+" "+"char"+d.b+" "+" float"+d.c+" "+"double"+d.d+" "+"String"+d.e+" "+"boolean"+d.f);
}
}
9.方法的重载:方便记忆
要求:方法名相同 参数必须不同(个数,类型,顺序)
例5. 为“无名的粉”写一个类:class WuMingFen 要求:
1.有三个属性:面码:String theMa 粉的份量(两):int quantity
是否带汤:boolean likeSoup
2.写一个构造方法,以便于简化初始化过程,如:
WuMingFen f1 = new WuMingFen(“牛肉”,3,true);
3.重载构造方法,使得初始化过程可以多样化:
WuMingFen f2 = new WuMingFen(“牛肉”,2);
4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的?
WuMingFen f3 = new WuMingFen();
5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
public class WuMingFen {
public String theMa;
public int quantity;
public boolean likeSoup;
public WuMingFen(String theMa,int quantity,boolean likeSoup){
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
public WuMingFen(String theMa,int quantity){
this.theMa = theMa;
this.quantity = quantity;
}
public WuMingFen(){
}
public void check(){
if(theMa==this.theMa && quantity==this.quantity && likeSoup==this.likeSoup){
System.out.println("符合要求");
}else{
System.out.println("不符合要求");
}
}
public static void main(String[] args) {
WuMingFen f1 = new WuMingFen("牛肉",3,true);
WuMingFen f2 = new WuMingFen("牛肉",2);
WuMingFen f3 = new WuMingFen( );
System.out.println(f1.theMa+" "+f1.quantity+" "+f1.likeSoup);
System.out.println(f2.theMa+" "+f2.quantity);
System.out.println(f3);
}
}
10.构造函数:构造方法 :
1)语法结构:public + 类名(参数列表){方法体}
2)作用:创建对象 初始化属性
3)当没有显示声明构造函数时,系统会默认提供一个空的构造函数(缺省构造函数)
4)当显示声明构造函数,系统则不提供
5)当形参名和属性名相同。用this区分
11.this:即将出现的对象
this():调用当前类构造函数,放在第一行
this.:调用属性或者方法
例6. 编写Java程序,用于显示人的姓名和年龄。
定义一个人类(Person),该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
在main方法中创建人类的实例,然后将信息显示。
public class Person{
private String name;
private int age;
public Person(String name,int age){
this.age =age;
this.name =name;
}
public void name(){
}
public void age(){
}
public void display(){
System.out.println("姓名"+name);
System.out.println("年龄"+age);
}
public static void main(String[] args) {
Person p= new Person("yhl", 22);
p.display();
}
}