
大家好,又见面了,我是你们的朋友全栈君。
面向对象的本质:以类的方式组织代码,以对象的形式组织(封装)数据
三大特征
在生活认识角度:先有对象,后有类。
在代码运行角度:先有类,后有对象。类是对象的模板,对象是类的实例。
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
//修饰符+返回值类型+方法名(参数列表){方法体 return}
public String sayHello(){
return "hello world";
}
public void hello(){
return;
//返回类型为void,可以不写return,也可以写return后面不跟东西
}
public int max(int a,int b){
return a>b?a:b;
}
}异常抛出
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) {
}
//在方法上抛出异常给调用方法的人用throws
public void readFile(String file) throws IOException {
}
public class Demo02 {
public static void main(String[] args) {
}
//在一个类中为什么static方法不能调用普通方法?
//而普通方法可以调用static方法?
-----------------------------------------------
//因为static方法时间片非常短,它的加载与类的加载是同步的
//而非静态方法要等到类实例化后才会加载
public static void a(){
b();//报错
}
public void b(){
a();//通过
}
}
//此处仅作示例,两个方法互相调用会造成死锁值传递和引用传递——java中只有值传递
public class Demo03 {
public static void main(String[] args) {
int a=1;
System.out.println(a);//输出1
Demo03.change(a);
System.out.println(a);
//此处为什么也是输出1?
//因为java是值传递,只是拷贝了一份传给方法
}
public static void change(int a){
a=10;
}//notes:
//一个类中只能有一个公共类public class且这个类名与文件名相同
//其他类不能加public
public class Demo04 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//此处输出null
Demo04.change(person);
System.out.println(person.name);//此处输出tim
}
public static void change(Person person){
person.name="tim";
}
}
class Person{
String name;
}this关键字————this代表当前类或者当前对象(继承,多态)
类和对象的关系
使用new关键字创建对象时

类的构造器 也称类的构造方法
构造器notes:
1.使用new关键字创建对象,该类必须要有构造器 2.构造器用来实例化属性(给属性赋初值)

IDEA快捷键 ——alt+insert生成构造器——constructor
public class Person {
String name;
int age;
public Person() {
//无参构造
}
public Person(String name) {
//有参构造
this.name = name;
}
public Person(String name,int age) {
//有参构造
this.name = name;
this.age=age;
}
}
//测试类
public class Application {
public static void main(String[] args) {
Person person = new Person("tim");
System.out.println(person.name);
}
}
超棒,建议回看p65 https://www.bilibili.com/video/BV12J41137hu?p=65
对象的引用
默认初始化:
整数:0 小数:0.0.
char:u0000
boolean: false
引用类型:null
属性私有——private,要想查看或修改仅能通过get/set
IDEA快捷键alt+insert——Getter and Setter
public class Person {
private String name;
private int age;
private char sex;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
//set方法中可以设置一些安全性检测
if(age>120||age<0){
this.age=-1;
}
else {
this.age = age;
}
}
}public class Application {
public static void main(String[] args) {
Person xiaoming = new Person();
xiaoming.setName("小明");
System.out.println(xiaoming.getName());
xiaoming.setAge(135);
System.out.println(xiaoming.getAge());
}
}封装的好处:
package oop.demo05;
public class Person {
public int money=10_0000_0000;
public void say(){
System.out.println("person类说了句话");
}
}Student类继承了Person类
package oop.demo05;
public class Student extends Person{
}package oop;
import oop.demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
//student本来没有say方法,它的say()是继承于Person类的
System.out.println(student.money);
}
}父类的私有属性,私有方法,无法被继承
修饰符的小结
在java中所有类,都直接或者间接继承object类


notes:
当父类的属性或方法被private修饰时,不能被继承
(也就是super.属性或super.方法()不能调父类的私有)

如果在父类中,写了有参构造方法,默认的无参构造方法就没了。此时子类在写super()去调用父类的无参构造就会报错了,所以一般情况:如果重写了父类的有参构造,一定要记得加上一个无参构造。
super小节
super&&this
这里主要指:继承父类之后,对父类方法的重写

(方法的调用只和左边有关,打错字了[doge]) 解释:调用对象方法时,这个方法到底是用的父类方法,还是子类方法,主要看这个对象是以哪一个类定义出来的,看等号左边那个类。


和上上张图对比看两次的运行结果差异

小结:
为什么需要重写?
父类的功能,子类不一定需要,或者不满足,需要改写其他功能
IDEA快捷键Alt+Insert——Override Methods
多态可以实现动态编译——增强程序的可扩展性
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student()
//new Person()
//但是一个对象可以指向的引用类型就不确定了
Student s1 = new Student();
Person s2 = new Student();//父类的引用指向子类
Object s3=new Student();
//对象能执行哪些方法,主要看左边的类型
//对象是执行父类的方法还是子类的方法?
//当两个类中都有同一个方法,则执行子类的
}
public class Person {
}
public class Student extends Person {
}
}s1是Student类型,能调用自己和父类的方法
s2是Person类型,它指向子类Student,它不能调用子类独有的方法
子类可以用父类的方法,父类用子类的方法要进行强制转换 (此处需要强制将Person类转换为Student类)—-可能不对,还需要理解
多态notes:
instanceof通过返回一个布尔值来指出,当前对象是否是一个特定类或者它的子类的一个实例
public class Application {
public static void main(String[] args) {
//假设存在如下继承关系
//Object>Person>Student
//Object>Person>Teacher
//Object>String
//===================================================================
Object object = new Student();
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person); //true
System.out.println(object instanceof Object); //true
System.out.println(object instanceof Teacher); //false
System.out.println(object instanceof String); //false
//===================================================================
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person); //true
System.out.println(person instanceof Object); //true
System.out.println(person instanceof Teacher); //false
// System.out.println(person instanceof String); 编译错误
// 因为person属于Person类,而Person类和String类没有继承关系
//===================================================================
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person); //true
System.out.println(student instanceof Object); //true
// System.out.println(student instanceof Teacher); // 编译错误
// System.out.println(student instanceof String); // 编译错误
}
}System.out.println( X instanceof Y )
编译能否通过? 取决于X,Y之间是否存在父子关系
得出结果? X是Y的子类型得到true,否则false
//继承关系
//Object>Person>Student
//Object>Person>Teacher Student类有一个go方法
public class Application {
public static void main(String[] args) {
//普通类型的转换
64->32 高转低(强制)————丢失精度
32->64 低转高(自动)
//引用类型的转换
//父类是更高级的类型,子类是更低级的
父->子 高转低(强制)
子->父 低转高(自动)————丢失子类特有的方法
//高 <---------------- 低 自动转换
Person obj=new Student();
// obj.go() 编译出错?因为obj已经被自动转换为了Person类型,丢失了go方法
// Person类型是Student类的父类,Person类并没有go()方法
// 所以如何让obj可以使用go方法?——把它强转为Student类型。
Student student1 = (Student) obj;
student1.go();//这两句也可以直接简化为 ((Student)obj)).go();
}
}小结:
修饰属性——静态属性
public class Student {
private static int age; //静态变量——和类同时加载,存在于堆中的方法区中
private double score; //非静态变量——实例化对象后才存在
public static void main(String[] args) {
Student student = new Student();
System.out.println(Student.age); //通过类名访问静态变量
// System.out.println(Student.score); //报错,通过类名——不能访问非静态变量
// (因为静态变量和类同时创建,非静态变量要等到对象实例化时候才创建,所以非静态变量只能通过实例对象来调用)
System.out.println(student.age); //通过对象访问静态变量
System.out.println(student.score); //通过对象访问非静态变量
}
}修饰方法——静态方法
public class Student {
//非静态方法
public void run(){
}
//静态方法
public static void go(){
}
public static void main(String[] args) {
run();//编译出错
go();//通过
// notes:
// 1.同理,静态方法和类同时创建,非静态方法要等到实例化后才能使用(new)
// 2.静态方法通过类名或者对象名都可以调用,非静态方法只能通过对象调用
// 3.此处比较特别,因为主函数和两个方法在同一个类中,所以不用前缀,也可以直接调用静态方法
// 4.在非静态方法中可以调用静态方法,然而在静态方法中不能调用非静态方法!
}
}public class Person {
//匿名代码块——可用于赋初值
{
System.out.println("匿名代码块");
}
//静态代码块——类一加载就会同时执行静态代码块中的内容,且只执行一次
static {
System.out.println("静态代码块");
}
//构造方法
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
}
}第一次执行: 输出: 静态代码块 匿名代码块 构造方法
====================================================== 第二次执行: 输出: 匿名代码块 构造方法
得出结论:类中模块的加载顺序——静态代码块,匿名代码块,构造方法 (且静态代码块只执行一次)
final所修饰的类,无法被继承
抽象类被继承后,它原有的所有抽象方法,在子类中必须都要进行重写(实现)
(如果子类也是抽象类,则可以不重写,让子子类再去重写)
抽象类的特点
Java类是单继承,但是接口可以实现“多继承” (此时继承也不叫继承了,叫实现——implements)
普通类:只有普通方法 抽象类: 有普通方法和抽象方法 接口:只有抽象方法——无法直接创建对象
接口的本质是契约——把规定写出来,哪个类要使用他的抽象方法,就实现(implements)这个接口,然后将其抽象方法重写具体实现。 IDEA快捷键Alt+Insert——Override Methods

接口中定义的属性——默认为常量(public static final 默认,可以不写)
抽象思维,需要锻炼——架构师
interface小结:


java.lang.Throwable
用户输入的内容格式不符合?要读取的文件不存在?内存满?被除数为0?文件找不到?网络连接失败?参数非法—–>Exception
三种异常
异常等级图——写在catch()中


关键字——try,catch,finally,throw,throws
public class Test {
public static void main(String[] args) {
// try {A} catch (B b) {C} finally{D}
//A是监控区域,如果A中出现了B类异常(b是临时变量名),则可以忽视A中的问题,执行C;如果A中没有出现B类异常将不会执行C
//D中语句无论如何都会执行
int a=1;
int b=0;
try {
System.out.println(a/b); //除数等于0肯定是会出现异常的
}catch (ArithmeticException e){
//算术异常
System.out.println("出现ArithmeticException");
}finally{
System.out.println("运算结束");
}
}
}运行结果:
出现ArithmeticException 运算结束
notes:
可以并列捕获多个异常——把更高级的异常写在更下面 (否则编译无法通过,因为更高级的异常包含了低级异常)
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
try {
System.out.println(a/b);
}catch (Error e){
System.out.println("出现Error");
}catch (Exception e){
System.out.println("出现Exception");
}catch (Throwable e){
System.out.println("出现Throwable");
}finally{
System.out.println("运算结束");
}
}
}对一行代码自动生成异常捕获代码 IDEA快捷键—— ctrl+alt+T

相比于自己手写,快捷键生成的异常处理块会多一句e.printStackTrace(),可以手动更改为其他处理语句,比如终止程序:System.exit()

关键字throw——主动抛出异常,在方法中使用,可以在方法中进行捕获

关键字throws——如果该方法处理不了这个异常,就只能在该方法上,往外抛异常(抛给调用方法的地方),然后在调用方法的代码块位置进行捕获。

自定义的异常需要继承Exception类(一般不需要自定义异常)

此处好像有一点点错误,右边框框里写的,此处是将异常抛给了调用者,所以好像应该去掉test()方法体内的那个主动抛出异常throw…下次看再分析

实践中的经验总结
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168486.html原文链接:https://javaforall.cn