目录
课前先导:
在刚开始学习java的时候啊,我们都知道它是面向对象编程的,可什么是对象呢?她和我们现实中想要的对象有什么关联呢?
一、什么是类?
类的定义:
具有相同属性和方法的多个对象的集合。
我们常说:鱼对鱼,虾对虾,乌龟找王八。这些说明什么:物以类聚人以群分。我们日常生活中为什么给一些人或物分类?是因为它们具有相同的属性。而鱼、虾、王八最后都可以用来被我们用来干嘛?起锅烧水。
比如我们两个人聊天:
我:我有一个朋友贼fool;
你:哎呦,我有一个兄弟更fool;
我:我朋友上回收拾厕纸用筷子夹......
你:我兄弟上次蹲坑没带纸,我给他送的筷子和勺子......
那这两个朋友就被我们按照一个属性——fool;归为了一个类——小可爱。朋友一般都有个共同的用法——坑。
二、类的创建
创建类:
[访问修饰符] class 类名{
//代码块
}
这里我们创建一个动物类(Animal);动物有什么属性?名字,性别,几条腿,可爱程度等;这些都可以定义,然后让它们自我介绍好吧。
public class Animal {
//定义属性
String name; //名字;
char sex; //性别;
int leg; //有几条腿;
double fool; //可爱程度。
//定义方法
public void selfIntroduction(){
System.out.println("大家好!我是"+name+","+sex+"性,有"+leg+"条腿"+fool+"分可爱。");
}
}
注意:这里是没有main方法的。
三、什么是对象?
定义:
现实世界中客观存在的实体都是对象(只要你能看得见、摸得着的物体都是对象),由属性和方法组成。万事万物皆对象。
上文我们定义了类,类中定义的属性和方法是这一种类共同拥有的;而一个种类中一般是由多个具体的个体组成的,而每个个体都是一个对象。
上问我们有一个种类——小可爱,我们的两个朋友被我们归类成了这个种类,那么朋友一是个对象,朋友二也是个对象。
类是对象的抽象,对象是类的具体
类是对象的模板,对象是类的特例
四、对象的创建、赋值;方法的调用
对象的创建:类名 对象名 = new 类名();
对象的赋值: 对象名.属性 = 属性值;
方法的调用: 对象名.方法名();
书接上文,我们新建一个动物的测试类(AnimalTest),在这个类中定义对象,并给这些对象的属性赋值,最后调用方法让它们完成自我介绍。
创建一个对象并赋值
public class AnimalTest {
public static void main(String[] args){
Animal animalOne = new Animal();//定义第一个对象:动物一(animalOne);
animalOne.name = "狗";
animalOne.sex = '雄';
animalOne.leg = 4;
animalOne.fool = 9.9;
animalOne.selfIntroduction();
}
}
定创建多个对象并赋值
public class AnimalTest {
public static void main(String[] args){
Animal animalOne = new Animal();//定义第一个对象:动物一(animalOne);
animalOne.name = "狗";
animalOne.sex = '雄';
animalOne.leg = 4;
animalOne.fool = 9.9;
animalOne.selfIntroduction();
Animal animalTwo = new Animal();//定义第二个对象:动物二(animalTwo);
animalTwo.name = "兔子";
animalTwo.sex = '雌';
animalTwo.leg = 4;
animalTwo.fool = 10;
animalTwo.selfIntroduction();
Animal animalThree = new Animal();//定义第三个对象:动物三(animalThere);
animalThree.name = "蜈蚣";
animalThree.sex = '雄';
animalThree.leg = 382; //蜈蚣种类繁多,腿也不一样多,大家自己去数一数。
animalThree.fool = 1;
animalThree.selfIntroduction();
}
}
运行结果
大家好!我是狗,雄性,有4条腿,9.9分可爱。
大家好!我是兔子,雌性,有4条腿,10.0分可爱。
大家好!我是蜈蚣,雄性,有382条腿,1.0分可爱。
五、定义类和对象的三种方法
(1)具有相同的属性和方法,不在同一个类。详情见上文。
(2)具有相同的属性和方法,写在同一个类;
public class Animal {
String name;
char sex;
int leg;
double fool;
public void selfIntroduction(){
System.out.println("大家好!我是"+name+","+sex+"性,有"+leg+"条腿,"+fool+"分可爱。");
}
public static void main(String[] args){
Animal animalOne = new Animal();
animalOne.name = "狗";
animalOne.sex = '雄';
animalOne.leg = 4;
animalOne.fool = 9.9;
animalOne.selfIntroduction();
}
}
(3)具有相同的属性,没有相同的方法,不写在同一个类
public class Animal {
String name;
char sex;
int leg;
double fool;
}
public class AnimalTest {
public static void main(String[] args){
Animal animalOne = new Animal();
animalOne.name = "狗";
animalOne.sex = '雄';
animalOne.leg = 4;
animalOne.fool = 9.9;
System.out.println("大家好!我是"+animalOne.name+","+animalOne.sex+"性,有"+animalOne.leg+"条腿,"+animalOne.fool+"分可爱。");
}
}
运行结果如下:
大家好!我是狗,雄性,有4条腿,9.9分可爱。
六、对象数组
(1)对象数组的数据类型就是具体的类名,对象数组存储的就是这个类的对象,每个数组元素就是一个对象;
(2)根据数组下标找到某个元素时,按照对象的使用方法来使用该元素。
类名[ ] 数组名 = {对象1,对象2,......对象n};
public class Animal {
//定义属性
String name; //名字;
char sex; //性别;
int leg; //有几条腿;
double fool; //可爱程度。
//定义方法
public void selfIntroduction(){
System.out.println("大家好!我是"+name+","+sex+"性,有"+leg+"条腿"+fool+"分可爱。");
}
}
public class AnimalTest {
public static void main(String[] args){
//创建第一个对象 狗
Animal animal01 = new Animal();
animal01.name = "狗";
animal01.sex = '雄';
animal01.leg = 4;
animal01.fool = 9;
//创建第二个对象 鸡
Animal animal02 = new Animal();
animal02.name = "鸡";
animal02.sex = '雄';
animal02.leg = 2;
animal02.fool = 3;
//创建第三个对象 兔子
Animal animal03 = new Animal();
animal03.name = "兔子";
animal03.sex = '雌';
animal03.leg = 4;
animal03.fool = 10;
//创建对象数组
Animal[] arr = {animal01,animal02,animal03};
//用对象数组元素的下标调用方法
arr[0].selfIntroduction();
arr[1].selfIntroduction();
arr[2].selfIntroduction();
//修改对象属性
System.out.println("\n**********修改对象属性***********");
arr[0].name = "猫";
arr[0].selfIntroduction();
}
}
七、来了,你们要的对象
作为男性,我在这里就定义一个女朋友,因为平台限制,就不定义太多属性了,你们想要什么属性自己定义哈
public class Girlfriend {
String name; //姓名
char sex; //性别
int age; //年龄
double height; //身高
int weight; //体重
public void selfIntroduction(){
System.out.println("大家好,我是"+name+","+age+"岁,身高"+height+",体重"+weight+"kg,是某人的"+sex+"朋友。");
}
public static void main(String[] args){
Girlfriend gf = new Girlfriend();
gf.name = "木南";
gf.sex = '女';
gf.age = 19;
gf.height = 170;
gf.weight = 48;
gf.selfIntroduction();
}
}
今天正式进入了面向对象的学习,第一课还是很简单的,大家先把今天的内容学会,把基础打好。好了,今天就学到这里,我是没颜、没钱、没对象的Loveletter,觉得内容不错的小伙伴可以点点关注,我们下篇博文见,再见!