zoukankan      html  css  js  c++  java
  • 抽象类和普通类的区别

    抽象类更像是提供一种模板。。

    抽象方法必须被子类实现,没有方法体。 

     抽象类的作用是普通类无法替代的,抽象类的作用主要是提供一种模版,比如我现在要写一个Person类.首先要是普通类我可能会这么写:

    public class Person{String age;int age;public void doJob(){

    System.out.println("i have a job")

    }}

    这里Person类有名字,年龄属性,还有一个介绍职业的方法,假如现在要写一个学生类,学生也是人要继承Person我们写Class Stndents{public void doJob{System.out.println("我们是学生")}},同理有一个教师类public class Teachers{public void doJob(System.out.println("我们是教师")){}},现在再来看下Person.里面的doJob方法有意义吗?i havea job这句话一点用处没有,所以可以将doJob写成dojob(),让具体类型的人具体实现,所以抽象类就起到了近一步抽象的作用,些Person类的时候我根本不需关心职业的问题我Person只提供一个抽象的模板

    参考资料:http://blog.csdn.net/mal327/article/details/7332110 

  • 相关阅读:
    Leetcode#179 Largest Number
    Leetcode#155 Min Stack
    Leetcode#14 Longest Common Prefix
    Leetcode#101 Symmetric Tree
    Leetcode#172 Fractorial Trailing Zero
    Leetcode#28 Implement strStr()
    Leetcode#46 Permutations
    Leetcode#48 Rotate Image
    Leetcode#134 Gas station
    Leetcode#137 Single Number II
  • 原文地址:https://www.cnblogs.com/brucetie/p/4195364.html
Copyright © 2011-2022 走看看