1. 程式人生 > >第五節:詳細講解Java中的介面與繼承

第五節:詳細講解Java中的介面與繼承

前言

大家好,我是 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知識點,有興趣可以繼續關注