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 成绩: 指导教师:娄嘉 ...
随机推荐
- 完全自制的五子棋人机对战游戏(VC++实现)
五子棋工作文档 1说明: 这个程序在创建初期的时候是有一个写的比较乱的文档的,但是很可惜回学校的时候没有带回来……所以现在赶紧整理一下,不然再过一段时间就忘干净了. 最初这个程序是受老同学所托做的,一 ...
- eclipse中maven项目部署到tomcat
其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...
- JVM的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集
(转自:http://my.oschina.net/u/436879/blog/85478) 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认 ...
- Linux内核基础--事件通知链(notifier chain)
转载: http://blog.csdn.net/wuhzossibility/article/details/8079025 http://blog.chinaunix.net/uid-277176 ...
- Python 学习记录
记录一些 学习python 的过程 -------------------------------------- 1. 初始学习 @2013年10月6日 今天开始学习python 了 遇到好多困难但是 ...
- eclipse中tomcat使用add and remove无法发布web项目
继上次启动eclipse中的tomcat报classNotFound的问题后,这次又遇到新问题.就是右键点击tomcat使用add and remove发布web项目至tomcat后,启动tomcat ...
- django 创建数据库表的linux命令
一旦你觉得你的模型可能有问题,运行 python manage.py validate . 它可以帮助你捕获一些常见的模型定义错误. 模型确认没问题了,运行下面的命令来生成 CREATE TABLE ...
- Entity Framework Architecture
http://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx The following figure shows ...
- 《OD大数据实战》mac下安装nginx+php
一.mac安装nginx + php + php-fpm 或apache + php 1. Mac 下 Nginx.MySQL.PHP-FPM 的安装配置 2. Mac下安装LNMP(Nginx+P ...
- Unable to locate package update
碰到这个问题后找到这个帖子就转了过来 当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是: ...