第五节:详细讲解Java中的接口与继承
2018-03-30 08:29
405 查看
前言
大家好,我是Vic,今天给大家带来
详细讲解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基础讲解09-----接口,继承,多态
- java中的接口与继承,接口的例子讲解
- javaee学习日记之java基础之接口,继承,抽象类
- 详细讲解装饰者模式和继承之间的关系和优劣(讲道理这事儿,不怕过 就怕不够)
- Java中的继承和接口
- Java面向对象-接口的特点和接口的继承
- 黑马程序员—JAVA基础之继承、抽象类、接口
- 澄清 Java 的接口与继承机制
- C\C++ 程序员从零开始学习Android - 个人学习笔记(八) - java基础 - 继承、抽象类、接口、内部类(待续)
- 黑马程序员_Java继承、多态、接口零碎知识总结
- 详细解析Java中抽象类和接口的区别(来自:java060515)
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]
- Java中的参数传递与对象Clone详细讲解
- Java基础——面向对象——继承、抽象类、接口、多态、包、内部类、异常等
- java动态代理jdk与cglib详细讲解
- Java的接口、继承
- 黑马程序员--------java面向对象 继承、接口、多态、Object类
- java接口的继承问题
- java一个好玩的接口讲解
- Java 动态代理机制分析及扩展_讲解比较详细,收藏