面向对象
1. 类
定义:有共同特点的对象的集合,是一个不占据任何空间的抽象概念,用class关键字来创建。
2.对象
定义:对象是把数据及数据的操作方法放在一起的整体,拥有属性和方法两种描述。
2.1 属性
描述对象的静态特点,定义及使用使用时没有括号;
//定义People类
public class People{
//定义age属性,name属性,及sex属性
String name;
int age ;
boolean sex = true ;//true为男性;
}
2.2 方法
描述对象的静态的功能,定义及使用时有括号(括号内可以有参数,也可以没有参数)且可能有返回值,若没有返回值用void定义。
//定义People类
public class People ;
//定义无返回值且无形参Add()方法,输出1+2的结果
public void Add(){
System.out.println(" 1+2 = " + (1+2) );
}
//定义返回int类型值却有形参Multiplication()方法:返回 x * y 的值;
public int Multiplication(int x , int y){
int multi = x * y ;
return multi ;
}
3. 面向对象的三大特性
3.1 封装
- 体现形式:方法、属性的私有化、内部类
- 属性的私有化:将属性用private修饰,然后提供对外的访问(getXXX)和设置(setXXX)的方法,在方法中进行限定,使属性值更加符合的场景要求
- 优势:提高代码的复用性,保证数据的合法性
限定变量或者方法的使用范围
修饰符 | 本类 | 同包 | 子类 | 其他包 |
---|---|---|---|---|
public | √ | √ | √ | √ |
protected | √ | √ | √ | × |
default(默认不写) | √ | √ | × | × |
private | √ | × | × | × |
3.2 继承
- 定义:继承是指从已有类的到继承信息创建新类的过程;提供信息的类称为父类(超类、基类);得到继承信息的类被称为子类(派生类);子类会自动拥有父类所有非私有的属性和方法;继承让变化中的软件系统有了一定的延续性,同时继承也是封装程序中可变因素的重要手段。
- 继承方式:
- 显示继承:通过extends关键字实现
- 隐式继承:不写extends关键字,默认继承Object类的属性和方法
3.3 多态
- 重载是指一个类中可以定义有相同的名字,但参数不同的多个方法。(不同的含义:参数类型,个数,顺序不同)
- 方法的重载实现的是编译时的多态;
public class MyMath {
int a ;
int b ;
//构造函数重载
public MyMath(){
}
public MyMath(int a){
this.a = a ;
}
public MyMath(int b , int a){
this.b = b ;
this.a = a ;
System.out.println(a+b);
}
public int add(int b ,double a){
return (int)(a + b) ;
}
//方法的重载
public int add(double a , int b){
return (int)(a+b) ;
}
public int add(int a , int b){
return a + b ;
}
public int add(int a , int b , int c){
return a + b + c ;
}
public static void main(String[] args) {
MyMath myMath = new MyMath(4,9) ;
System.out.println(myMath.add(3, 5));
System.out.println(myMath.add(5.60, 6));
System.out.println(myMath.add(6, 5.60));
}
}
- 方法的重写是运行时的多态;
- 方法的重写在子类中可以根据需要从基类中继承来的方法进行重写;
- 重写方法必须和被重写的方法具有相同方法名称、参数列表和返回类型;
- 重写的方法不能使用比被重写方法更严格的访问权限(由于多态)
public class People {
public void sing(){
System.out.println("大家可以唱歌");
}
}
public class Student extends People{
@Override
public void sing() {
super.sing();
System.out.println("学生学习唱歌");
}
}
public class TestStudent {
public static void main(String[] args) {
People people = new People() ;
People people1 = new Student() ;
people.sing(); //执行People类的方法
people1.sing();//执行Student类的方法
}
}
- 向上转型:
- 父类 对象名 = new 子类构造器;父类引用指向子类
- 接口 接口名 = new 实现类构造器;接口引用指向其实现类
- 抽象类 对象名 = new 实现类构造器;抽象类引用指向其实现类
this关键字
- 普通的直接引用
- 形参与成员名字重名,用this来区分
public class Person {
private String name ;
private int id ;
private int age ;
public Person(String name ,int id , int age){
this.name = name ;
this.id = id ;
this.age = age ;
}
}
- 引用本类的构造函数
public class Person {
private String name ;
private int id ;
private int age ;
public Person(){
}
public Person(String name) {
this.name = name ;
}
public Person(String name ,int id ){
this(name) ;
this.id = id ;
}
public Person(int id){}
public Person(int id , int age){
this(id) ;
}
public Person(String name ,int id , int age){
this.name = name ;
this.id = id ;
this.age = age ;
}
}
- this不能用于static方法:
- 普通方法中,this总是指向调用该方法的对象;
- 构造方法中,this总是指向正要初始化的对象。
super关键字
- 普通的直接引用
super相当于是指当前对象的父类的引用,用super.XXX来引用父类的成员。 - 子类中的成员变量或方法与父类中的成员变量或方法同名时,用super进行区分。
public class People {
protected String name ;
public People(String name) {
this.name = name ;
}
}
public class Student extends People{
private String name ;
public Student(String name , String name1) {
super(name);
this.name = name1 ;
}
public void getInfo(){
System.out.println(this.name); //student
System.out.println(super.name); //people
}
}
public class TestStudent {
public static void main(String[] args) {
Student student = new Student("student","people") ;
student.getInfo();
}
}
输出结果如下:
- 引用父类的构造函数
- super(参数) :调用父类中的某一个构造函数 (必须写在构造函数体内的第一条语句)
- this(参数):调用本类中另一种形式的构造函数(必须写在构造函数体内的第一条语句)
static关键字
- 作用:
- 建立静态代码块优化性能;
- 直接通过类名调用属性和方法,省去创建一个新的对象;
- 应用:
- 修饰代码块、成员属性、成员方法、内部类;
注:不允许修饰局部变量
- 修饰代码块、成员属性、成员方法、内部类;
public class Student{
String name ;
int id ;
//static关键字修饰成员属性
static int age ;
//static关键字修饰成员方法(构造函数不是静态方法)
public static void printAge(){
System.out.println(age) ;
}
public void study(){
printAge() ;
System.out.println(name+"在学习") ;
}
public void sayHello(String sname){
System.out.println(name+"向"+sname+"早上好!") ;
}
}
public class Test {
public static void main(String[] args){
//被static关键字修饰,类名直接调用属性和方法
Student.age = 25 ;
Student.printAge() ;
//未被static修饰,创建对象调用属性和方法
Student s1 = new Student() ;
s1.study();
s1.sayHello("kid");
}
}
输出结果如下:
final修饰符
- final修饰类,不能被继承;
- final修饰属性,值不能被更改;
- final修饰方法,方法不能被重写;
- final修饰局部变量,变成常量,值不能被更改;
- final修饰形参时,即形参在方法内部不能被修改。