一 参数
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();
-
}
-
}
-
----