// 父类
class Animal{
void run(){
System.out.println("动物在奔跑");
}
}
// 子类
class Cat extends Animal{
// 重写父类中的run()方法,
void run(){
System.out.println("猫在抓老鼠");
}
}
public class Demo3 {
public static void main(String[] args) {
Cat c1 = new Cat();
c1.run();
}
}
5.4 对象的类型转换
(1)向上转型
父类的引用指向子类的对象;
(2)向下转型
子类的引用指向父类的对象;
e.g.
// 基类
class Person{
void run(){
System.out.println("在跑步");
}
}
// 子类
class Student extends Person{
@Override
void run() {
System.out.println("学生在跑步");
}
// 子类中定义的方法
void study(){
System.out.println("学生在学习");
}
}
public class Demo1 {
public static void main(String[] args) {
// 向上转型:父类的引用指向子类的对象
Person p1 = new Student();
p1.run();
// p1.study(); 不能调用子类中定义的成员
// 向下转型:子类的引用指向父类的对象
Student s1 = (Student) p1;
s1.run();
s1.study();
}
}
关于对象类型转换的两点说明:
(1)向上转型对象只能访问从基类继承的成员及重写的方法,子类中定义的成员不能被访问;
(2)向上转型可以减少重复代码,提高系统的扩展性;
5.5 多态性
多态指同一种行为(方法)具有多种表现形态;
(1)编译时多态(静态的)
主要指方法的重载,编译时通过方法参数列表不同来进行区分;
(2)运行时多态(动态的)
同一类型的引用使用不同的实例而执行不同的操作,产生不同的结果; 实现方式:继承、接口
class Animal{
void run(){
System.out.println("动物在奔跑");
}
}
class Cat extends Animal{
void run(){
System.out.println("猫在抓老鼠");
}
}
class Dog extends Animal{
void run(){
System.out.println("狗在奔跑");
}
}
public class Demo2 {
void show(Animal a){
a.run();
}
public static void main(String[] args) {
Animal a = new Animal();
// 向上转型
Animal a1 = new Cat();
Animal a2 = new Dog();
Demo2 d2 = new Demo2();
// 多态的表现
d2.show(a);
d2.show(a1);
d2.show(a2);
}
}
5.6 可变长参数
e.g.
public class Demo3 {
int f(int x,int y){
System.out.println("+++++++++");
return x+y;
}
int f(int x,int y,int z){
return x+y+z;
}
// // 与第1、2个方法构成重载
// int f(int[] x){
// int sum=0;
// for(int k:x){
// sum+=k; // sum=sum+k;
// }
// return sum;
// }
// 与第1、2个方法构成重载;与第3个方法不构成重载,系统认为这两个方法等价
int f(int ... x){
int sum=0;
for(int k:x){
sum+=k; // sum=sum+k;
}
return sum;
}
public static void main(String[] args) {
Demo3 d3 = new Demo3();
System.out.println(d3.f(new int[]{25,30,40,50,60}));
System.out.println(d3.f(20,30));
}
}
public class Demo4 {
static void f(String ... str){
String s1 = "";
for(String s : str){
s1 +=s;
}
System.out.println(s1);
}
public static void main(String[] args) {
f("Hello"," ","Java","!");
}
}
5.7 抽象类
使用abstract修饰的类称为抽象类;
e.g.
// 抽象类
abstract class A{
// 抽象方法
abstract double area(int r);
}
class B extends A{
// 实现抽象类中的抽象方法
double area(int r){
return Math.PI*r*r;
}
}
public class Demo3 {
public static void main(String[] args) {
B b = new B();
System.out.println(b.area(5));
System.out.println(b.area(10));
}
}
e.g.
// 抽象类
abstract class A{
int r;
int w,h;
// 构造器
A(int r){
this.r = r;
}
A(int w,int h){
this.w = w;
this.h = h;
}
// 抽象方法
abstract double area();
}
class B extends A{
B(int r){
super(r);
}
// 实现抽象类中的抽象方法
double area(){
return Math.PI*r*r;
}
}
class C extends A{
C(int w,int h){
super(w,h);
}
// 实现抽象类中的抽象方法
double area(){
return w*h;
}
}
public class Demo3 {
public static void main(String[] args) {
B b = new B(3);
System.out.println(b.area());
C c = new C(4,5);
System.out.println(c.area());
}
}
public interface Itf1 {
// var: 默认是 public static final的
double PI=3.1415926;
// abstract method: 默认是 public abstract的
double area(int r);
// static method: 默认是 public 的
static void f(){
System.out.println("这是接口中的静态方法");
}
}
// 实现接口 Itf1
public class Demo7 implements Itf1{
// 实现接口中的抽象方法
public double area(int r) {
return PI*r*r;
}
public static void main(String[] args) {
// 创建类的实例(对象)
Demo7 d7 = new Demo7();
double s = d7.area(5);
System.out.println("圆的面积为:"+s);
// 调用接口中的静态方法
Itf1.f();
}
}
关于接口的几点说明:
①接口中的变量会隐式指定为public static final的,即全局常量;
②接口中的抽象方法会隐式指定为public abstract的;
③接口中不能定义代码块、构造器;
④接口中可以定义静态方法(默认是public的),但不能被继承;
⑤接口支持多继承;类只支持单继承;
⑥一个类可以实现多个接口。
e.g.
public interface Itf2{
double E = 2.71828;
}
// 接口的多继承
public interface Itf3 extends Itf1,Itf2{
}
// 一个类可以实现多个接口,实现接口 Itf1,Itf2,Itf3
public class Demo7 implements Itf1,Itf2,Itf3 {
// 实现接口中的抽象方法
public double area(int r) {
return PI*r*r;
}
public static void main(String[] args) {
// 创建类的实例(对象)
Demo7 d7 = new Demo7();
double s = d7.area(5);
System.out.println("圆的面积为:"+s);
}
}
*5.9 枚举
枚举是一种特殊的类,一般表示一组常量;
e.g.
public enum Season {
Spring,Summer,Autumn,Winter;
}
public class Demo8 {
public static void main(String[] args) {
Season s1 = Season.Spring;
System.out.println(s1);
System.out.println(Season.Winter.ordinal());
for (Season k : Season.values()){
System.out.println(k);
}
}
}
interface BH{
void show();
}
public enum Color implements BH{
RED("红色",0),BLACK("黑色",1);
String name;
int index;
Color(String name,int index){
this.name = name;
this.index = index;
}
public void show(){
System.out.println(index+":"+name);
}
public static void main(String[] args) {
Color c1 = Color.BLACK;
System.out.println(c1.index);
System.out.println(c1.name);
c1.show();
}
}