Java横向三天复习—面向对象
1.成员变量+成员属性+成员方法
public class Test {
//成员变量
String color;//颜色
int speed;//速度
int seat=5;//座位 共同变量
//成员方法
public void run(String color,int speed,int seat){
this.color=color;
this.speed=speed;
this.seat=seat;
}
public static void main(String[] args) {
//创建对象
Test test=new Test();
test.color="黄色";
test.speed=110;
System.out.println("这辆车的颜色是"+test.color+"这辆车的行驶速度是"+test.speed+"满载客为:"+test.seat);
test.run("白色",120,3);
System.out.println("这辆车的颜色是"+test.color+"这辆车的行驶速度是"+test.speed+"满载客为:"+test.seat);
}
}
2.构造方法
String color;
int speed;
int seat;
//Java会自动的赠送给每一个类一个无参数的构造方法
//如果自定义了构造方法,java就不再赠送你了
//在创建对象的时候,自动调用方法
public Test(String color,int speed,int seat){
this.color=color;
this.speed=speed;
this.seat=seat;
}
public void run(){
System.out.println("这个车的颜色->"+this.color+"怎么那么鲜艳"+"速度为:"+this.speed+"座位是:"+this.seat);
}
public static void main(String[] args) {
Test test=new Test("白色",120,3);
test.run();
}
}
3.构造方法重载
public class Test {
String name;
String waihao;
int age;
String bangPai;
public void run(){
System.out.println(this.age+this.bangPai+this.waihao+this.name);
}
public Test(String name,String waihao,int age){
this.name=name;
this.waihao=waihao;
this.age=age;
}
public Test(String name,String waihao,int age,String bangPai){
this(name,waihao,age);
this.bangPai=bangPai;
}
public static void main(String[] args) {
Test test=new Test("雷云腾","是最帅的",19,"");
Test test2=new Test("雷云帅","是最帅的",18,"华山派");
test.run();
test2.run();
}
}
4. 静态Static
-
被Static修饰的成员变量是共享的
public class Test { String name; static String country="大清";//它是共享地方 String address; public Test(String name,String address){ this.name=name; this.address=address; } public static void main(String[] args) { Test test=new Test("赵铁柱","八大胡同"); Test test1=new Test("李小花","朝阳门"); Test.country="明国"; System.out.println(country); } }
不推荐使用test.country="明国"这种静态变量,
推荐使用类名去访问静态的内容
特点:
1.数据共享
2.属于类的,并不属于对象
3.优先对象产生的
通用构造器,静态构造器
构造器执行顺序:静态构造器->通用构造器->构造方法
5.导包
包:其实本质上就是文件夹
在代码中需要写package包名;
导包:import 包+类
6. 访问权限
1.public公共的,所有人都可访问
2.default包访问权限,在自己包内可以随意访问。
3.private 私有的。
用的最多的就是public和private
7.getter和setter
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W3oZZcjF-1655219809634)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614170633205.png)]
定义了getter和setter可以在别的文件里面调用
成员变量一般使用private来声明,保护成员变量不被胡乱的赋值
setter:主要是给成员变量赋值,做一定的保护
getter:从成员变量中获取数据。
public class Test {
//成员变量是私有的
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
if (this.name=="雷云腾"){
System.out.println("恭喜你,好帅");
}else{
System.out.println("查无此人");
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age<0){
System.out.println(this.age=0);
}else{
System.out.println(this.age=age);
}
}
public static void main(String[] args) {
Test test=new Test();
test.setName("雷云腾");
test.setAge(1);
}
}
在set里面设置条件,在get里面得到数据,在程序的入口里面输入条件
8.继承
继承:子类可以自动拥有父类中除了私有内容外的其他所有内容。
public class 类名 extends 父类{
}
作用:简化代码的开发
子类对父类进行拓展,
子类继承了父类的构造方法,成员方法,除私有方法外。还可在子类中重写父类的方法。
9.super关键字
super:表示父类中的内容
this:表示自己类中的内容
用super和 this来区分父类和子类中重名的内容
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zlQvIlLh-1655219809635)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614182636737.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-muVXxFcP-1655219809636)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614182755854.png)]
小总结:
1.super可以获取到父类中的内容
2.可以调用父类中的构造方法,必须写在子类构造方法的第一行,如果父类的构造是无参的,可以不写,如果父类没用无参数的构造,必须要写super
10.方法的重写
重写:子类对父类中提供的方法进行重新定义
语法:子类和父类中的方法的声明完全一致
重写又被称为方法的覆盖
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F44aBtnV-1655219809637)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614184948384.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vTZtOaHe-1655219809637)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614184911991.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t5YptqSV-1655219809638)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614185010634.png)]
11.多态性
把子类的对象赋值给父类的引用(变量)向上转型
多态性:把不同的数据类型进行统一
1.把子类的对象赋值给父类的变量 ->向上转型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Lsdqqcq-1655219809638)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614192542319.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eezmtgyT-1655219809639)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614192626188.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mMzYF4Oi-1655219809639)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614192656839.png)]
2.把父类的变量转化回子类的变量- >向下转型
向下转型有风险,Java要求必须写强制类型转换
12.final
1.被final修饰的变量不可以被改变
2.被final修饰的变量不可以被改变,又被称为常量
3.被final修饰的方法不可以被重写。
4.被final修饰的类不可以被继承。
13.abstract抽象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eWEpfknp-1655219809640)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614200010236.png)]
抽象方法:使用abstract来修饰,直接用;结束
抽象类:如果一个类中有抽象方法,这个类必须是一个抽象类
特点:
1.抽象类不可以创建对象
2.抽象类的子类,必须重写父类中的方法。否则子类必须也是抽象类
抽象类可以有正常的方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UoFMhKmO-1655219809640)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614200609722.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AzjWgilM-1655219809641)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614200655918.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NpthCwHs-1655219809641)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614200700771.png)]
14.接口
//接口中所有的方法都是抽象方法,可以省略到abstract
接口中所有内容都是公开的,公共的
接口中所有的方法都是抽象方法。不可以有正常的方法
能继承接口的只能是接口
接口和类只能实现关系 implements
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r4M3PcPZ-1655219809641)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614203959916.png)]
15.toString
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FBlAkCRC-1655219809642)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614205038451.png)]
09640)]
[外链图片转存中…(img-AzjWgilM-1655219809641)]
[外链图片转存中…(img-NpthCwHs-1655219809641)]
14.接口
//接口中所有的方法都是抽象方法,可以省略到abstract
接口中所有内容都是公开的,公共的
接口中所有的方法都是抽象方法。不可以有正常的方法
能继承接口的只能是接口
接口和类只能实现关系 implements
[外链图片转存中…(img-r4M3PcPZ-1655219809641)]
15.toString
[外链图片转存中…(img-FBlAkCRC-1655219809642)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sF4rODW5-1655219809642)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220614205052195.png)]