java接口输入参数返回值_[java]类&抽象类&接口分别作为方法参数和返回值

一 参数

fd4b20dcdc1b3225383544d203d82731.png

1,类名作为参数

package com.ma.demo;

public class People {

void show() {

System.out.println("people show");

}

}

---

package com.ma.demo;

public class PeopleDemo {

void method(People p) {

p.show();

}

}

---

package com.ma.demo;

public class Test {

public static void main(String[] args) {

People p = new People();

PeopleDemo pd = new PeopleDemo();

pd.method(p);

}

}

----

2,抽象类做为参数

package com.ma.demo2;

abstract class People {

abstract void show();

}

---

package com.ma.demo2;

public class PeopleDemo {

void method(People p) {

p.show();

}

}

---

package com.ma.demo2;

public class Student extends People {

@Override

void show() {

// TODO Auto-generated method stub

System.out.println("student show");

}

}

---

package com.ma.demo2;

public class Test {

public static void main(String[] args) {

People p = new Student();

PeopleDemo pd = new PeopleDemo();

pd.method(p);

}

}

------

3,接口做为参数

package com.ma.demo3;

interface People {

public abstract void show();

}

---

package com.ma.demo3;

public class PeopleDemo {

void method(People p) {

p.show();

}

}

---

package com.ma.demo3;

public class Student implements People {

@Override

public void show() {

// TODO Auto-generated method stub

System.out.println("student show");

}

}

---

package com.ma.demo3;

public class Test {

public static void main(String[] args) {

People p = new Student();

PeopleDemo pd = new PeopleDemo();

pd.method(p);

}

}

------

二.返回值

1,类名作为返回值

package com.ma.demo4;

public class People {

void show() {

System.out.println("people show");

}

}

---

package com.ma.demo4;

public class PeopleDemo {

People method() {

return new People();

}

}

---

package com.ma.demo4;

public class Test {

public static void main(String[] args) {

PeopleDemo pd = new PeopleDemo();

People p = pd.method();

p.show();

}

}

------

2,抽象类作为返回值

package com.ma.demo5;

abstract class People {

public abstract void show();

}

---

package com.ma.demo5;

public class PeopleDemo {

Student method() {

return new Student();

}

}

---

package com.ma.demo5;

public class Student extends People {

@Override

public void show() {

// TODO Auto-generated method stub

System.out.println("student show");

}

}

---

package com.ma.demo5;

public class Test {

public static void main(String[] args) {

PeopleDemo pd = new PeopleDemo();

People p = pd.method();

p.show();

}

}

----

3,接口作为返回值

package com.ma.demo6;

interface People {

public abstract void show();

}

--

package com.ma.demo6;

public class PeopleDemo {

People method() {

return new Student();

}

}

--

package com.ma.demo6;

public class Student implements People {

@Override

public void show() {

// TODO Auto-generated method stub

System.out.println("student show");

}

}

----

package com.ma.demo6;

public class Test {

public static void main(String[] args) {

PeopleDemo pd = new PeopleDemo();

People p = pd.method();

p.show();

}

}

----

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值