Java 面向对象概念
Interface 接口
An interface defines a protocol of communication between two objects.
An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
A class that implements an interface must implement all the methods declared in the interface. 接口中的方法都是implicitly abstract。
An interface name can be used anywhere a type can be used.
Polymorphism 多态
不同的子类overridden 父类的同一方法,每个子类具有单独的实现。
Instance Methods(实例方法)vs Class Methods (类方法)
public class Animal {
public static void testClassMethod(){
System.out.println("The class method" + " in Animal");
}
public void testInstanceMethod(){
System.out.println("The instance method"+" in Animal ");
}
}
public class Cat extends Animal {
/*
*The overriding method has the same name, number and type of parameters, and return type as the method it overrides
*/
//Class Method类方法,需要通过类名来调用,子类hidden父类的方法
public static void testClassMethod(){
System.out.println("The class Method in Cat");
}
//Instance Method实例方法,通过类的实例来调用,子类overridden父类的方法。
public void testInstanceMethod(){
System.out.println("The instance Method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
myAnimal.testInstanceMethod(); // 总是调用覆盖过的子类方法
super.testInstanceMethod(); //可以通过super来调用父类被覆盖的方法。
Animal.testClassMethod(); // 取决于是子类调用,还是父类调用
}
}
super
super 可用来调用父类被覆盖的方法。
super 可用来调用父类构造器constructor。
默认自动调用父类无参构造器,可以显示调用含参数的构造器。
Final
final 可用来声明方法不可被覆盖,类不可被继承。
final static用于定义不变的变量。
Abstract Methods and Classes
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon),like
//含有抽象方法的类必须为抽象
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
子类必须实现了父类的所有抽象方法,否则子类必须也为抽象,留给它的子类去实现剩余的抽象方法。
Abstract Classes vs Interfaces
abstract classes can contain fields that are not static and final, and they can contain implemented methods.
If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way.
abstract class can implements an interface, 提供部分的方法实现。
抽象方法不能被实例化。
Java 面向对象概念的更多相关文章
- java面向对象概念1
一.java面向对象学习的三条主线: 1.java类及类的成员:属性.方法.构造器:代码块.内部类 2.面向对象的三大特征:封装性.继承性.多态性.(抽象性) 3.其它关键字:this.super.s ...
- java————面向对象概念
面向对象 OO:面向对象 OOP:面向对象编程 OOA:面向对象分析 OOD:面向对象设计 面向对象的特征 继承,封装,多态 什么是对象? 对象是存在的具体实体,具有明确定义的特征和行为. 万物皆对象 ...
- java面向对象概念2
一.理解“万事万物皆对象”. 1.在java语言范畴中,我们都将功能.结构等封装到类中,通过类的实例化,来调用具体的功能结构. 2.涉及到java语言与前端html.后端的数据库交互时,前后端的结构在 ...
- Java面向对象-面向对象编程之基本概念
面向对象这个概念,每本书上的说法定义很多. 我自己根据我的经验,自己归档总结了下, 所谓面向对象,就是 以基于对象的思维去分析和解决问题,万物皆对象: 面向对象经常和面向过程放一起讨论: 这里举例, ...
- java面向对象1-面向对象概念
面向对象概念-类与对象的关系 封装:指隐藏对象的属性和实现细节,仅对外提供公共访问方式,private-构造方法/构造器-this关键字-static关键字(javadoc制作工具类) -代码块 继承 ...
- 087 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 01 封装的概念和特点
087 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 01 封装的概念和特点 本文知识点:封装的概念和特点 说明:因为时间紧张,本人写博客过程中只是对 ...
- 谈谈Java面向对象的三大特性
Java面向对象的三大特性就是指封装.继承.多态了. 一.封装: 概念:封装是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. (举例:笔记本电脑就是一个封装体,Java语言中最小的封装体就是函数 ...
- java基础1.0::Java面向对象、面向对象封装、抽象类、接口、static、final
一.前言 一直以来都是拿来主义,向大神学习,从网上找资料,现在就把自己在工作中和学习中的所理解的知识点写出来,好记星不如烂笔头,一来可以作为笔记自己温习,二来也可以给走在求学之路的同学们一点参考意见, ...
- JAVA课程实验报告 实验二 Java面向对象程序设计
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:韩玉琪 学号:20135317 成绩: 指导教师:娄嘉 ...
随机推荐
- HDU 4634 Swipe Bo 状态压缩+BFS最短路
将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...
- c# 获取数组中最大数的值
求数组中最大的数的值:1.数组的max函数: class Program { static void Main(string[] args) { ,,,,,,,,,}; int max= GetMax ...
- 【转】Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例
原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例 部分代码参考http ...
- django-based blog- mezzanine
django-based blog- mezzanine zinnia 博客 hydra 暴力破解
- Hadoop集群(第2期)_机器信息分布表
1.分布式环境搭建 采用4台安装Linux环境的机器来构建一个小规模的分布式集群. 图1 集群的架构 其中有一台机器是Master节点,即名称节点,另外三台是Slaver节点,即数据节点.这四台机器彼 ...
- Oracle数据库之三
子查询 -- 就是在一个查询中包含多个select语句(一般就2个) select id,first_name,dept_id from s_emp; 想查询和Ben一个部门的员工的id,first_ ...
- 【温故知新】c#异步编程模型(APM)--使用委托进行异步编程
当我们用到C#类许多耗时的函数XXX时,总会存在同名的类似BeginXXX,EndXXX这样的函数. 例如Stream抽象类的Read函数就有 public abstract int Read(byt ...
- SQL多表联合分页.....
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go /* 支持多表查询分页存储过程(事理改进)2012.3 --多表联查1 declare @Count int ...
- UVa 10878 Decode the tape
题目很简单,代码也很短.第一遍做的时候,我居然二乎乎的把input里面的小框框忽略掉了,所以WA了一次. 每一行代表一个二进制的ASCII码,'o'代表1,空格代表0,中间的小黑点忽略. 我直接把一行 ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...