Java随堂笔记06-Java面向对象
Java随堂笔记06-Java面向对象
面向过程&面向对象
面向过程思想
- 步骤清晰简单,第一步做什么,第二步做什么...
- 面向过程适合处理一些较为简单的问题
面向对象思想
- 物以类聚,分类的思维模式,首先思考解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思考
- 面向对象适合处理复杂的问题,适合处理多人协作的问题
对于描述复杂的事物,为了从宏观上把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是具体到微观操作,仍然需要面向过程的思路去处理
什么是面向对象
- 面向对象编程(Object-Oriented Programming,OOP)
- 面向对象编程的本质就是:以类的方式组织代码,以对象的方式组织(封装)数据
- 抽象
- 三大特性:
- 封装
- 继承
- 多态
- 从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
- 从代码运行角度考虑是先有类后有对象。类是对象的模板
静态方法和非静态方法
public static void a(){ //静态方法 和类一起加载
b(); //报错
}
public void b(){ //非静态方法 类实例化以后才存在
}
IDEA快捷键:Alt + Insert 快速创建构造方法,set、get方法等
定义了有参构造之后,如果还想使用无参构造,就需要把无参构造显式定义出来
三大特性
封装
- 我们程序设计要追求“高内聚,低耦合” 。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用
- 封装(数据的隐藏)
- 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
- 属性私有,get/set
继承
-
Java中只有单继承,没有多继承
-
继承是类与类之间的一种关系,除此之外,类与类之间的关系还有依赖、组合、聚合等
-
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends
-
Object类 Java中所有类都直接或间接继承Object类
-
super 调用父类的属性或方法 子类对象创建时会先调用父类的构造方法
-
重写
-
public class Father{ public static void say(){ System.out.println("father is saying"); } public void write(){ System.out.println("father is writing"); } } -
public class Son extends Father{ public static void say(){ System.out.println("son is saying"); } public void write(){ System.out.println("son is writing"); } } -
public static void main(String[] args){ Father father = new Father(); father.say(); //"father is saying" father.write(); //"father is writing" Father son = new Son(); son.say(); //"father is saying" son.write(); //"son is writing" }
-
多态
public class Father{
public void say(){
System.out.println("father is saying");
}
}
public class Son extends Father{
public static void say(){
System.out.println("son is saying");
}
public void write(){
System.out.println("son is writing");
}
}
public static void main(String[] args){
Son son1 = new Son();
Father son2 = new Son();
son1.say(); //"son is saying"
Son2.say(); //"son is saying"
son1.write(); //"son is writing"
//son2.write(); //报错
((Son) son2).write(); //"Son is writing"
}
instanceof和类型转换
instanceof用于比较一个对象和其他类是否相似
//Object > String
//Object > Person > Student
//Object > Person > Teacher
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); //编译报错
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); //编译报错
代码块
public class Person{
//匿名代码块 可以用来为对象赋初始值
{
System.out.println("匿名代码块");
}
//静态代码块
static {
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args){
Person person1 = new Person();
/* 输出结果:
静态代码块
匿名代码块
构造方法
*/
System.out.println("=================");
Person person2 = new Person();
/* 输出结果:
匿名代码块
构造方法
*/
/*
静态代码块只被执行一次。静态代码块和匿名代码块执行在构造方法之前
*/
}
}
抽象类
- abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,n那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类
- 抽象类中可以没有抽象方法,但是有抽象方法的类一定是抽象类
- 抽象类,不能实例化对象,它是用来让子类继承的
- 抽象方法,只有方法的声明,没有方法的实现,需要继承的子类去重写
- 子类继承抽象类,那么就必须要重写抽象类中的抽象方法,除非该子类也是抽象类
接口
-
关键字 interface 接口中一般全是方法的声明,需要类去实现
-
类实现接口关键字implements
-
接口可以“多继承”,即一个类可以实现多个接口
-
接口中方法的默认修饰符是public abstract,可以省略不写
-
接口中常量默认修饰符是public static final,一般接口中不写常量
内部类
几种常见的内部类
public class Outer{
private int id;
public void out(){
System.out.println("outer")
}
//局部内部类
public void method(){
class A{
public void ini(){
}
}
}
public class Inner(){
public void in(){
System.out.println("inner")
}
}
//匿名内部类
new UserService{ //UserService userService = new UserService
@override
public void hello(){
}
};
}
class B{ //一个文件中最外边只能有一个public修饰的类
//...
}
interface UserService{
void hello();
}
public static void main(String[] args){
Outer outer = new Outer(); //外部类实例化
Outer.Inner inner = Outer.new Inner(); //内部类实例化
}

浙公网安备 33010602011771号