一.封装(数据的隐藏)
- 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
- 记住这句话就够了:属性私有,get/set
package MIanxing;
/*
封装的优点:
1.提高程序点的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加了
*/
//属性私有 private
public class Demo02 {
private String name;
private int age;
//get 获得这个数据
public String getName() {
return name;
}
//set 给这个数据设置值
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>100 || age<0)
{
age=-1;
}else{
this.age = age;
}
}
public static void main(String[] args) {
Demo02 de= new Demo02();
de.setName("脆脆鲨");
System.out.println(de.getName());
de.setAge(999);
System.out.println(de.getAge());
}
}
二.继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
- extends的意思是“扩展”,子类是父类的扩展。
- Java中只有单继承,没有多继承!!!
- 继承是类与类之间的一种关系。除此之外,类与类之间的关系还有依赖,组合,聚合等。
- 继承关系的两个类,一个为子类(派生类),一个为父类(基类),子类继承父类,使用关键字extends来表示。
- object类
- super关键字
- 方法重写
package java3;
//测试类
public class Demo01 {
public static void main(String[] args) {
Students s=new Students();
System.out.println(s.money);
s.say();
}
}
//父类(基类)
class Person{
int money=100;
public void say(){
System.out.println("深海脆脆鲨*");
}
}
//子类(派生类) :学生 is 人
//子类继承了父类,就会拥有父类的全部方法
class Students extends Person{
}
(1)object类
ctrl+H
在java中,所有的类,都默认直接或者间接继承object,object是Java为所有类提供的基类。(没有显示的继承)
(2)super和this
注意点
- super调用父类的构造方法,必须在构造方法的第一行
- super必须只能出现在子类的方法或者构造方法中
- super和this不能同时调用构造方法
Vs this: - 代表的对象不同:
this:本身调用着这个对象
super:代表父类对象的应用
前提:
this:没有对象也可以使用
super:只能在继承条件下才可以使用
构造方法:
this():本类的构造
super():父类的构造
package java3;
//测试类
public class Demo01 {
public static void main(String[] args) {
Students s=new Students();
s.test("我鲨了你");
s.test1();
}
}
//父类(基类)
class Person{
String name="深海脆脆鲨*";
public void print(){
System.out.println("person");
}
}
//子类(派生类)
class Students extends Person{
//ctrl+H
String name="小熊妮";
public void print() {
System.out.println("student");
}
public void test(String name){
System.out.println(name); //我鲨了你
System.out.println(this.name); //小熊妮
System.out.println(super.name); //深海脆脆鲨*
System.out.println("------------");
}
public void test1(){
print(); //students
this.print(); //students
super.print(); //person
}
}
```java
package java3;
//测试类
public class Demo01 {
public static void main(String[] args) {
Students s=new Students();
}
}
//父类(基类)
class Person{
public Person() {
System.out.println("person无参执行了");
}
}
//子类(派生类)
class Students extends Person{
//ctrl+H
public Students() {
//隐藏代码:调用了父类的无参构造
super(); //调用父类构造器,必须要在子类构造器的第一行
System.out.println("students无参执行了");
}
}
(3)方法重写
- 重写需要有继承关系,子类重写父类的方法!
- **方法重写规则:
-
- 方法名相同,参数列表相同,返回值类型相同。
-
- 修饰符(访问权限)不能小于父类。public>protected>default>private
-
- 抛出的异常范围可以缩小,但不能扩大;**
- 子类的方法和父类必须要一致,方法体不同。
- 为什么要重写呢:父类的功能,子类不一定需要,或者不一定满足。
package java3;
public class Demo02 {
public static void main(String[] args) {
B b=new B();
b.say();
//父类的引用指向了子类
A a=new B();//子类重写了父类的方法
a.say();
}
}
//父类
class A{
public static void say()
{
System.out.println("A-say:we are one");
}
}
class B extends A{
//Override重写都是方法的重写,与属性无关
//重写 有功能的注释
public static void say() {
System.out.println("B-say:EXO");
}
}
package java3;
public class Demo02 {
public static void main(String[] args) {
B b=new B();
b.say();
//父类的引用指向了子类
A a=new B(); //子类重写了父类的方法
a.say();
}
}
//父类
class A{
public void say()
{
System.out.println("A-say:we are one");
}
}
class B extends A{
//Override重写都是方法的重写,与属性无关
//重写 有功能的注释
public void say() {
System.out.println("B-say:EXO");
}
}
小贴士:
针对加static的静态方法:编译看左边,执行也看左边
*********对于非静态方法:编译看左边,执行看右边
三.多态
- 即同一方法可以根据发送对象的不同二采用多种不同的行为发式。
- 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多。
- 多态存在的条件:
- 1. 有继承关系
- 2. 子类重写父类方法
- 3. 父类引用指向子类对象:father f1=new son(); - 注意:多态是方法的多态,属性的没有多态性
- instanceof
package java3;
//测试类
public class Demo01 {
public static void main(String[] args) {
// Students能调用的方法都是自己的或者父类的。
Students s1=new Students();
//Person父类型,可以指向子类,但是不能调用子类独有的方法
Person s2=new Students();
Object s3=new Students();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
s1.eat();
//强制转换
((Students) s2).eat();//子类重写了父类的方法,执行子类的方法
}
}
//父类(基类)
class Person{
public void run(){
System.out.println("人在跑步");
}
}
//子类(派生类)
class Students extends Person{
public void eat() {
System.out.println("学生在吃饭");
}
public void run() {
System.out.println("学生在跑步");
}
}
instanceof
- 自动转型:Father f=new Son(); 子继承父,向上转型,子类型自动自动转为(上升为)父类类型。
- 强制转型:Son s=(Son)Father; 向下转型,父类类型转为子类自己的类型
package java3;
public class Demo03 {
public static void main(String[] args) {
//Object>String
//Object>Person>Students
//Object>Person>Teachers
Object ob=new Son();
System.out.println(ob instanceof Son); //true
System.out.println(ob instanceof Father); //true
System.out.println(ob instanceof Daughter); //false
System.out.println(ob instanceof String); //false
System.out.println(ob instanceof Object); //true
System.out.println("-------------");
Father fa=new Son();
System.out.println(fa instanceof Father); //true
System.out.println(fa instanceof Son); //true
System.out.println(fa instanceof Daughter); //false
//System.out.println(fa instanceof String); //编译报错
System.out.println(fa instanceof Object); //true
System.out.println("-------------");
Son so=new Son();
System.out.println(so instanceof Son); //true
System.out.println(so instanceof Father); //true
//System.out.println(so instanceof Daughter); //编译报错
// System.out.println(so instanceof String); //编译报错
System.out.println(so instanceof Object); //true
}
}
class Father{
}
class Son extends Father{
}
class Daughter extends Father{
}