Java期末复习—接口、继承、多态

2021/5/30 20:52:09

本文主要是介绍Java期末复习—接口、继承、多态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

类的继承

举个栗子,电脑可以上网,打游戏,听音乐 等等 ;而平板电脑是基于电脑的基础上扩展出来的,它也可以上网,打游戏,听音乐等,同时它还可以拍照、充电等一些电脑没有的功能 ;这里就可以称平板电脑是电脑的继承 ;

给个见到的简答题:什么是继承?
答:通过必要的说明能够实现某个类无需重新定义就拥有另一个类的某些属性和
方法,并把这种关系称为继承,先定义的类称为父类,后定义的类称为子类,并
且允许多层的继承关系。

extends关键字

要体现继承的特性,要用到extends关键字 ;

语法:child extends parents

public class Computer {
    String screen = "液晶" ;
	void start() {
		System.out.println("开机") ;
	}
}
public class Pad extends Computer {
    int dian = 90 ;
    void Open(){
        System.out.println("开机") ;
    }
}
public class Demo {
	public static void main(String[] args) {
		Computer com = new Computer() ;
		Pad pad = new Pad() ;
        System.out.println(com.screen) ;
		com.start();
        System.out.println(pad.screen) ;
		pad.start();
        System.out.println(pad.dian) ;
		pad.Open();
	}
}

方法重写

当我们要用平板和电脑同时打开一张图片时,电脑要用鼠标点击,平板则点击触摸屏就可实现,这就涉及两种相同方法的不同实现,就要用到方法的重写 ;

public class Computer {
	void show() {
		System.out.println("点击") ;
	}
}
public class Pad extends Computer {
	void show() {
		System.out.println("触摸") ;
	}
}
public class Demo {
	public static void main(String[] args) {
		Computer com = new Computer() ;
		Pad pad = new Pad() ;
		com.show();
		pad.show();
	}
}

super关键字

当方法重写后,原父类的方法就被覆盖掉了,此时就用到super关键字来调用原父类 ;

语法:

super.property ;

super.method() ;

public class Pad extends Computer {
	String name = "Siri" ;
	public Pad() {
		super() ; //初始化 ,前面不能写东西
		this.name = super.name ;
	}
	String start() {
		return super.start()+",Siri" ;
	}
}
public class Computer {
	String name = "Computer" ;
	String start() {
		return "Hi" ;
	}
}
public class Demo {
	public static void main(String[] args) {
		Computer com = new Computer() ;
		Pad pad = new Pad() ;
		System.out.println(com.start()) ;
		System.out.println(pad.start());
		System.out.println(pad.name) ;
	}
}

在Java语言中,一个类只能有一个父类。

当要实现一个子类有两个父类的情况,要用到多重继承,也就是说,让两个都想成为它父类的方法彼此成为子类和父类 ,也就是让其中一个成为了它的父类的父类 ;

public class Demo {
	public static void main(String[] args) {
		child ch = new child() ;
		ch.show();
	}
}
class parent1 {
	void show() {
		System.out.println("父类的父类") ;
	}
}
class parent2 extends parent1 {

}
class child extends parent2 {
 	
}

子类不仅能覆盖父类的方法,还能覆盖父类的属性 。

public class Demo {
	public static void main(String[] args) {
		child ch = new child("pob") ;
		System.out.println(ch.name) ; //marry
	}
}
class parent {
	String name = "amy" ;
	public parent(String name) {
		this.name = name ;
	}
}
class child extends parent {
	String name = "marry" ; //虽然与父类属性相同,但仍属于子类独有的特性;
	public child(String name) {
		super(name);
	}
}

Object类

Object类是所有类的父类,

Object的常用方法:

getClass() ; //返回对象执行时的class实例 ;

public class Demo {
	public static void main(String[] args) {
		Object[] arr = new Object[4] ;
		arr[0] = new Object() ; //class java.lang.Object
		arr[1] = new String("abc") ; //class java.lang.String
		arr[2] = new Integer(123) ; //class java.lang.Integer
		arr[3] = new Demo() ; //class haut.ch10.Demo
		for(Object i:arr)
			System.out.println(i.getClass()) ;
	}
}

toString() ; //将对象返回为字符串形式 ;

public class Demo {
	public static void main(String[] args) {
		Object[] arr = new Object[4] ;
		arr[0] = new Object() ; //java.lang.Object@2f92e0f4
		arr[1] = new String("abc") ; //abc
		arr[2] = new Integer(123) ; //123
		arr[3] = new Demo() ; //Demo类
		for(Object i:arr)
			System.out.println(i.toString()) ;
			//System.out.println(i) ;
	}
	public String toString() {
		return "Demo类" ;
	}
}

equals() ; //比较两个对象是否相等 ;

比较的是两个对象的地址是否相等 ;

public class Demo {
	public static void main(String[] args) {
		Object[] arr = new Object[4] ;
		arr[0] = new Object() ; 
		arr[1] = new String("abc") ; 
		arr[2] = arr[0] ; 
		arr[3] = new Object() ;
		System.out.println(arr[2].equals(arr[0])) ; //true
		System.out.println(arr[0].equals(arr[3])) ; //false
	}
}

对象的转型

类的向上转型 ,父类声明的对象由子类来声明;

public class Demo {
	public static void main(String[] args) {
		Person people = new Student("marry") ; //Hello,marry
	}
}
public class Person {
	public Person(String name) {
		System.out.println("Hello,"+name) ;
	}
}
public class Student extends Person {
	public Student(String name) {
		super(name);
	}
}

类的向下转型,将父类强制转化为子类的对象 ;

public class Demo {
	public static void main(String[] args) {
		Person people = new Student("marry") ; //Hello,marry
		Student stu = (Student)people ; //将people强制转化为Student
		System.out.println(people instanceof Student) ; //true
	}
}

a instanceof b ;判断a是不是b的子类 ;

方法的重载

方法名相同,参数个数不同 ;

方法名相同,参数顺序不同 ;

方法名相同,参数类型不同 ;

public class Demo {
	public static void main(String[] args) {
		System.out.println(add(1)) ;
		System.out.println(add(1.0)) ;
		System.out.println(add(1 , 2)) ;
		System.out.println(add(1 , 2.0)) ;
		System.out.println(add(1.0 , 2)) ;

	}
	static int add(int a) {
		return a ;
	}
	static double add(double a) {
		return a ;
	}
	static int add(int a , int b) {
		return a+b ;
	}
	static int add(double a , int b) {
		return (int)(a+b) ;
	}
	static int add(int b , double a) {
		return (int)(a+b) ;
	}
}

这里题目集里有个简答题:请简述重载和重写的区别
答:方法的重写 Overriding 和重载 Overloading 是 Java 多态性的不同表现。重写 Overriding 是父类与子类之间多态性的一种表现,重载 Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被“屏蔽”了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。Overloaded 的方法是可以改变返回值的类型。

多态

同一个变量,同一种方法,执行出不同的结果 ;

class Stars {
	void Yellow() {
		
	}
}

class Sun extends Stars {
	void Yellow() {
		System.out.println("红色") ;
	}
}

class Moon extends Stars {
	void Yellow() {
		System.out.println("黄色") ;
	}
}

public class Demo {
	public static void main(String[] args) {
		Stars little = new Sun() ;
		little.Yellow() ; //红色
		little = new Moon() ;
		little.Yellow(); //黄色
	}
}

抽象类

例如创建一个颜色类,颜色这个的概念是抽象的,不清楚它是哪一种颜色,所有此时要由abstract来修饰创建抽象类 ;

抽象类必须被继承 ;

抽象类对象不能进行实例化 , 如下Color c = new Color() 是不被允许的 ;

public class Demo {
	public static void main(String[] args) {
		Color c = new Red() ;
		Color d = new Yellow() ;
		c.show() ;
		d.show() ;
	}
}
public abstract class Color {
	public void show() {
		
	}
}
class Red extends Color {
	public void show() {
		System.out.println("红色") ;
	}
}
class Yellow extends Color {
	public void show() {
		System.out.println("黄色") ;
	}
}

抽象方法,

抽象方法必须被抽象类的子类所重写 ;

abstract class Name {
    abstract void name() ; //没有{} ;
}

接上面的栗子,  

public class Demo {
	public static void main(String[] args) {
		Color c = new Red() ;
		Color d = new Yellow() ;
		c.show() ;
		d.show() ;
	}
}
public abstract class Color {     //抽象类
	abstract public void show() ; //抽象方法
}
class Red extends Color {
	public void show() {
		System.out.println("红色") ;
	}
}
class Yellow extends Color {
	public void show() {
		System.out.println("黄色") ;
	}
}

接口

使用interface来定义一个接口。接口定义和类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成。

public class Demo {
	public static void main(String[] args) {
		Draw d = new Painting() ;
		d.draw(); //painting a picture
		Draw r = new Photo() ;
		r.draw(); //take a photo
	}
}
public interface Draw { //定义一个接口
	public void draw() ;
}
public class Painting implements Draw {
	public void draw() {
		System.out.println("painting a picture") ;
	}
}
public class Photo implements Draw {
	public void draw() {
		System.out.println("take a photo") ;
	}
}

写完的时候发现前面的不小心被删掉了,又写了一遍就写的多少有点简陋了 ≡(▔﹏▔)≡ 



这篇关于Java期末复习—接口、继承、多态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程