前言

大家好,给大家带来详细讲解Java中的接口与继承的概述,希望你们喜欢

什么是接口(interface)

接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象

interface Student{
public void read();
public void write();
} class ChineseStudent implements Student{
//复写
public void read(){
System.out.println("read");
}
public void write(){
System.out.println("write");
}
} class Test{
public static void main(String args[]){
ChineseStudent chinesestudent = new ChineseStudent();
Student student = chinesestudent; student.read();
student.write();
}
}
实现接口用implements关键字,
一个接口可以实现多个接口,
一个接口可以继承多个接口
interface Student{
public void read();
public void write();
} interface Teacher{
public void teach();
public void test();
} class Person implements Student,Teacher{
public void read(){
System.out.println("read"):
}
public void write(){
System.out.println("write");
}
public void teach(){
System.out.println("teach"):
}
public void test(){
System.out.println("test"):
}
} class Test{
public static void main(String args[]){
Person person = new Person(); Student student = person;
student.read();
student.write(); Teacher teacher = person;
teacher.teach();
teacher.close();
}
}

swith( char byte short int)只允许四种类型

public class Test{
public static void main(String args[]){
int score = 90;
if(score > 85 && score <= 100){
System.out.println("成绩为优");
}
else if(score > 75 && score <= 85){
System.out.println("成绩为良");
}
else if(score > 60 && score <= 75){
System.out.println("成绩为中");
}
else if(score <= 60 && score >= 0){
System.out.println("成绩为差");
}
else if(score > 100 || score < 0){
System.out.println("成绩不在正常的范围之内");
}
}
}

对象就是引用数据类型

class Test{
public static void main(String args[]){
Dog d = new Dog();
d.name="哈哈";
d.age=2;
d.jump();
System.out.println("名字是"+d.name);
}
}

重载的表达

class A{
void funA(){
System.out.println("没有参数的funA函数");
}
void funA(int i){
System.out.println("有参数的funA函数");
}
void funA(int i,double d){
System.out.println("拥有两个参数的funA函数");
}
}

什么是继承

在现实世界当中,继承就是儿子得到老子的东西,在面向对象的世界当中,继承就是一个类得到了另一个类当中的成员变量和成员方法

Java只支持单继承,不允许多继承,继承是为了减少重复代码

使用super调用父类构造函数的方法

class Person{
String name;
int age;
Person(){
System.out.prinltn("Person的无参数构造函数");
}
Person(String name,int age){
this.name=name;
this.age=age;
System.out.println("Person有参数的构造函数");
} void eat(){
System.out.println("定义吃饭的方法");
}
}
class Student extends Person{
//子类继承父类
Student(){
//父类
super();
System.out.println("Student的无参数构造函数");
}
Student(String name,int age,int id){
super(name,age);
this.id=id;
}
}

在Java中的继承,其实就是继承全部属性和方法(除了构造方法),除了private修饰的变量或者方法,子类无法进行访问

什么是复写

具有父子关系的两个类中,父类和子类各有一个函数,这两个函数的定义(返回值类型,函数名,参数列表)完全相同

对象的转型(多态性地体现)

父类引用指向子类对象,同一个类型,调用同一个方法,却能呈现不同的状态

  • 什么是向上转型:

    向上转型就是将子类的对象赋值给父类的引用。
  • 什么是向下转型:

    向下转型就是将父类的对象赋值给子类的引用。
Student s1 = new Student();
Person p = s1;
Student s2 = (Student)p;

类的多态

  • 父类引用指向子类对象
  • 调用的方法有重写

所谓的转型

  • 类型转换指:

    把一个引用所指向的对象的类型,转换为另一个引用的类型
  • 没有继承关系的类型进行转换,一定会失败

了解Object类

  • Object类是所有类的父类
  • Object类提供一个toString方法,返回当前对象的字符串表达
  • equals() 判断两个对象的内容是否相同

详解final

  • final可以修饰类(该类不能够被继承),成员变量(修饰基本类型变量,该变量只有一次赋值机会,修饰引用,该引用只有一次指向对象的机会),成员方法(不能够被重写)
  • String类是public final class String,不能被继承

什么是抽象函数

没有函数体的函数被称为抽象函数

什么是抽象类

使用abstract定义的类称为抽象类

  • 抽象类不能够生成对象
  • 抽象类不能实例化,继承抽象类,那么该类必须为抽象类
  • 一个类被声明为抽象类,不能够被直接实例化
abstract class Person{
abstract void eat();
} class Chinese extends Person{
void eat(){
System.out.pritln("hhh");
}
} class Test{
public static void main(String args[]){
Person p = new Chinese();
p.eat();
}
}
abstract class Person{
Person(){
System.out.println("Person没有参数的构造函数");
} Person(String name,String age){
this.name=name;
this.age=age;
} String name;
int age;
void introduce(){
System.out.println("我的名字是"+name+",我的年龄是"+age);
}
abstract void eat();
}
} class Chinese extends Person{
String address;
Chinese(){
super();
System.out.println("Chinese的构造函数");
} Chinese(String name,int age,String address){
super(name,age);
this.address=address;
}
void eat(){
//复写
System.out.println("吃饭");
}
} class Test{
public static void main(String args[]){
Person p = new Chinese();
p.eat();
}
}

如何生成内部类的对象

class Test{
public static void main(String args[]){
A a = new A(); A.B b = new A().new B();
//或者A.B b = a.new B();
}
} class A{
int i;
class B{
int j; int funB(){
int result = i+j;
return result;
}
}
} class Test{
public static void main(String args[]){
A a = new A();
A.B b = a.new B(); a.i=3;
a.j=1; int result = b.funB();
System.out.println(result);
}
} class A{
int i;
class B{
int j;
int funB(){
int result = A.this.i+this.j;
return result;
}
}
}

内部类

内部类有 非静态,静态,匿名类

语法: new 外部类().new 内部类()

匿名内部类

interfacce A{
public void doSomething();
} class B{
public void fun(A a){
System.out.println("B函数");
a.doSomething();
}
} class Work implements A{
public void doSomething(){
System.out.println("doSomething");
}
} class Test{
public static void main(String args[]){
Work work = new Work();
A a = work; B b = new B();
b.fun(a);
}
}
class Test{
public static void main(String args[]){
B b = new B();
b.fun(new A(){
public void doSomething(){
System.out.println("匿名内部类");
}
});
}
}

总结

  • 本文讲了详细讲解Java中的接口与继承,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注

第五节:详细讲解Java中的接口与继承的更多相关文章

  1. 第四节:详细讲解Java中的类和面向对象思想

    前言 大家好,给大家带来详细讲解Java中的类和面向对象思想的概述,希望你们喜欢 类和面向对象 在Java中怎样理解对象,创建对象和引用:什么是引用,对于基础学习的同学,要深入了解引用.示例:Stri ...

  2. 第九节:详细讲解Java中的泛型,多线程,网络编程

    前言 大家好,给大家带来详细讲解Java中的泛型,多线程,网络编程的概述,希望你们喜欢 泛型 泛型格式:ArrayList list= new ArrayList(); ArrayList list= ...

  3. 第七节:详细讲解Java中的日期,java.util.date

    前言 大家好,给大家带来详细讲解Java中的日期,java.util.date的概述,希望你们喜欢 类Date Java.lang.Object->java.util.Date public c ...

  4. 第六节:详细讲解Java中的装箱与拆箱及其字符串

    前言 大家好,给大家带来详细讲解Java中的装箱与拆箱及其字符串的概述,希望你们喜欢 装箱与拆箱 封装类有:Byte , short , Integer , Character , long , Fl ...

  5. 第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架

    前言 大家好,给大家带来详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的概述,希望你们喜欢 JAVA 异常 try...catch...finally结构的使用方法 class Tes ...

  6. 详细讲解Java中方法的重载和重写

    首先讲讲方法的重载: Java的重载就是在类中可以创建多个方法,它们具有相同的名字,但是却有不同的参数. 判断是否重载只有两个条件: 1)相同的方法名 2)不同的参数 具体为: A.方法参数类型不同 ...

  7. java中的接口与继承,接口的例子讲解

    extends 继承类:implements 实现接口. 简单说: 1.extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承, 2.JAVA中不支持多重继 ...

  8. 【转】详细讲解Java中log4j的使用方法

    转载地址:http://www.233.com/Java/zhuangye/20070731/142625631.html 1.Log4j是什么? Log4j可以帮助调试(有时候debug是发挥不了作 ...

  9. 详细讲解JAVA中的IO流

    一.流的概念        流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等.        ...

随机推荐

  1. 软工作业1—java实现wc.exe

    github项目地址 https://github.com/liyizhu/wc.exe WC 项目要求 基本功能列表: wc.exe -c file.c     //返回文件 file.c 的字符数 ...

  2. 外部tomcat发布springboot项目步骤和异常处理:java.lang.NoClassDefFoundError: javax/el/ELManager

  3. idea在debugger模式下无法启动,但是在run模式下可以启动的问题

    debugger模式下,启动idea,总是报内存溢出异常, Error creating bean with name 'sysRoleUserMapper' defined in URL [jar: ...

  4. squid日志详解

    quid的日志很重要.常常要了解的,其中最重要的就是命中率啦,不然反向代理做的用就不大. cat access.log|gawk ‘{print $4}’|sort|uniq -c|sort -nr ...

  5. Shell中的数组及其相关操作

    http://blog.csdn.net/jerry_1126/article/details/52027539 Shell中数据类型不多,比如说字符串,数字类型,数组.数组是其中比较重要的一种,其重 ...

  6. 使用kbmmw 生成REST 服务OpenAPI函数原型

    我们以前介绍了很多kbmmw 开发REST 的例子.一直有个问题困惑着大家. 我们提供REST 服务,如何让客户端快速的使用,当然可以写文档,但是一旦 后台改变了,又要再一次给调用者发新文档,非常的麻 ...

  7. SVN用法及常见问题分析

    SVN中英文对比: 1,今天遇到的新问题,在父节点里面找不到子节点文件夹,在子节点里面可以上传但是却一直上传不上去. 具体原因:子文件夹里面有个.svn文件(打开隐藏的项目可见),是的子文件夹的svn ...

  8. js实现(可实现局部打印)

    1.js实现(可实现局部打印) <input id="btnPrint" type="button" value="打印" oncli ...

  9. JavaWeb三大组件之Filter

    对请求或者响应进行拦截,并做额外的逻辑处理 filter能在一个请求访问目标资源之前对其进行拦截然后做某些逻辑处理,例如权限检查,也可以在一个输出响应到达客户端之前对其进行拦截并做一些额外的操作(例如 ...

  10. bootstrap-datepicker简单使用

    粗略整理,可能存在其他的方式请大家多多指教 选择年份 html <div class="dropdown"> <label class="search- ...