面向对象编程(java语言描述)k

继承

class Animal{
    private String name;

    public Animal(String name) {
        this.name = name;
    }
    public void eat(String food){
        System.out.println(name + " 正在吃 " + food);
    }
}

class Cat extends Animal{

    //Cat也包含name,是private类型
    public Cat(String name) {
        // ! this.name = name;
        //name有继承过来,但是是private修饰的,不能直接访问,需要调用基类构造器方法
        super(name);
    }
}

继承:目的为了代码重用,把公共的代码,放到一个单独的类之中,让其他的类,继承这个单独的类,单独的这个类里面包含的属性和方法,都自动被被新类支持进去。
并且,新的类还可以做出一些根据自己特色的扩展

Animal : 父类/基类/超类
Cat: 子类/派生类

父类和子类的关系,称为“is-a”

语法规则:

  1. 对于父类的private的字段和方法,子类中是无法访问的。
  2. Java中一个子类只能继承一个父类
  3. 子类的实例中,也包含着父类的实例,可以用super关键字得到父类实例的引用。

在这里插入图片描述

gettname()也可以用protected方法替代,protected:不同包的子类中也能使用!

package package1;

import package2.Animal;
//Animal类中的name用protected修饰
public class Bird extends Animal {
    public Bird(String name) {
        super(name);
    }
    public void fly(){
        System.out.println( name + " 正在飞 ");
    }
}

访问控制权限使用的原则:
尽可能的进行封装
private default pritected public

字段设为private,方法需要具体考虑,该设成public就设成public,有时候也设成private

final

final 修饰一个变量,表示这是一个常量
final 修饰一个类,表示这个类是不能被继续继承的。

final public class ChineseGardenCat extends Cat
//其他的类就不能继承 ChineseGardenCat

组合

has - a 关系

public class School {
    private Teacher teacher ;
    private Student[] students;
}

根据语义选择组合或者继承“has - a ” 和“is - a ”

多态

向上转型:类型转换,类和类之间的关系(父类和子类)

public class Test1 {
    public static void main(String[] args) {
        Bird bird = new Bird("圆圆");
        Animal animal = bird;//直接赋值
    }
}

两个不同的类型相互赋值

一个子类的对象,可以使用一个父类的引用去表示

public class Test1 {
    public static void main(String[] args) {
        Bird bird = new Bird("圆圆");
        Animal animal = bird;//直接赋值
        bird.eat("谷子");
        animal.eat("小米");
    }
}

bird 和animal 都指向 Bird对象,都是鸟在吃

向上转型的三种常见体现形式。

  1. 直接赋值
  2. 作为方法的参数
  3. 作为方法的返回值
public static void main(String[] args) {
        Bird bird = new Bird("圆圆");//实参
        feed(bird);
		
		Animal animal = findMyAnimal();
    }
    public static void feed(Animal animal){
    	//animal是形参,实际引用的是Bird类型对象
        animal.eat("谷子");
    }
    public static Animal findMyAnimal(){
        Bird bird = new Bird("圆圆");
        return bird;
    }

动态绑定

父类和子类可能会包含名字相同,逻辑不同的方法。
调用这个方法的时候,到底执行的是 父类的方法 还是 子类的方法,是在程序运行过程中确定的
动态指的是“运行时”而不是编译期

public class Test1 {
    public static void main(String[] args) {
        Animal animal = new Bird("圆圆");
        animal.eat("谷子");
    }
}

与后面的(new )有关

子类也有eat,父类也有eat,方法的覆写(名相同,逻辑不同)

  1. 方法名相同
  2. 参数必须完全相同(个数和类型)
  3. 返回值没有要求,一般让返回值一样
  4. 子类的方法访问权限控制不应该父类的小

多态:是一种思想方法,多态落实到代码上具体的体现形式,就是通过一下这三点来体现的。

  1. 向上转型
  2. 动态绑定
  3. 方法重写
package package4;
class Shape{
    public void draw(){
        //啥都不画
    }
}
class Circle extends Shape{
    @Override
    public void draw() {
        System.out.println("⚪");
    }
}
class Rect extends Shape{
    @Override
    public void draw() {
        System.out.println("□");
    }
}
class Flower extends Shape{
    @Override
    public void draw() {
        System.out.println("♣");
    }
}
public class Test2 {
    public static void main(String[] args) {
        //向上转型
        Shape s1 = new Circle();
        Shape s2 = new Rect();
        Shape s3 = new Flower();
        drawShape(s1);
        drawShape(s2);
        drawShape(s3);
    }
    public static void drawShape(Shape s){
        s.draw();
    }
}

多态是一种代码编写的方式,一个引用可以表现出不同的形态,
Flower Rect Circle 这些类是“类的实现者”编写的
Test 这个类(drawShape 方法)“类的调用者”编写的。
类的调用者不必关心当前类是啥类(直接s.draw())
就让类的调用者对类的实现细节知道的更少了

封装的更进一步
前面说的封装,是为了让类的调用者不需要知道类的实现细节(还是得知道这个类是啥类)
多态这里的类的调用者都不需要知道这个类是啥类,只需要知道这个类有一个draw()方法就可以了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本资源包括以下内容: 1、从官方网站下载的全书代码 2、Modern.Compiler.Implementation.in.Java.Second.Edition.chm Last year you may have seen the Modern Compiler Implementation in C: Basic Techniques (1997) which was the preliminary edition of our new 1998 textbook, Modern Compiler Implementation in C. The new, expanded version of this textbook describes all phases of a modern compiler: lexical analysis, parsing, abstract syntax, semantic actions, intermediate representations, instruction selection via tree matching, dataflow analysis, graph-coloring register allocation, and runtime systems. It includes good coverage of current techniques in code generation and register allocation, as well as functional and object-oriented languages, that are missing from most books. In addition, more advanced chapters are now included so that it can be used as the basis for two-semester or graduate course. The most accepted and successful techniques are described in a concise way, rather than as an exhaustive catalog of every possible variant. Detailed descriptions of the interfaces between modules of a compiler are illustrated with actual C header files. The first part of the book, Fundamentals of Compilation, is suitable for a one-semester first course in compiler design. The second part, Advanced Topics, which includes the advanced chapters, covers the compilation of object-oriented and functional languages, garbage collection, loop optimizations, SSA form, loop scheduling, and optimization for cache-memory hierarchies. A unique feature of the book is a well designed compiler implementation project in Java, including front-end and 'high-tech' back-end phases, so that students can build a complete working compiler in one semester. Accompanying support software is available.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值