1. 类名作为形式参数
将new出来的学生对象当作形式参数进行传递
class student{
public void eat(){
System.out.println("就是爱吃肉啊");
}
}
class studentdemo{
public void method(student s){
s.eat();
}
}
class 类名作为形式参数 {
public static void main(String args[]){
//普通的
student stu=new student();
stu.eat();
//类名作为形式参数
studentdemo studemo=new studentdemo();
student stud=new student();
studemo.method(stud);
}
}
2. 抽象类名作为形式参数
要注意的是抽象类不能实例化,所以要进行继承,对其子类进行操作
abstract class Student{
public abstract void study();/*{
System.out.println("一生认准一个人");
*/}
class Student1 extends Student{
@Override
public void study(){
System.out.println("就是你了啊");
}
}
class Teacher{
public void method(Student1 s){
s.study();
}
}
public class 抽象类名作为形式参数 {
public static void main(String[] args) {
Teacher teacher = new Teacher();
Student1 student1child = new Student1();
teacher.method(student1child);
}
}
3. 接口名作为形式参数
接口名作为形式参数
接口也不能直接实例化,需要先实现接口
interface dent{
public abstract void study();
}
class wo implements dent{
@Override
public void study(){
System.out.println("好好学习的最初愿望,就是你啊");
}
}
class laoshi{
public void method(wo s){
s.study();
}
}
public class 接口名作为形式参数 {
public static void main(String[] args) {
laoshi laoshi = new laoshi();
wo wo = new wo();
laoshi.method(wo);
}
}
- 类名作为返回值类型
class Yang{
public void study(){
System.out.println("梦里寻她,今在何方");
}
}
class Ding{
public Yang love(Yang Y){
Y.study();
return Y;
}
}
public class 类名作为返回值类型 {
public static void main(String[] args) {
Ding ding = new Ding();
Yang yang = new Yang();
ding.love(yang);
}
}
- 抽象类名作为返回值类型
abstract class A{
public abstract void study();
}
class B extends A{
@Override
public void study() {
System.out.println("我到了,你快来接我啊");
}
}
class C{
public B method(B b){
b.study();
return b;
}
}
public class 抽象类名作为返回值类型 {
public static void main(String[] args) {
B b = new B();
C c = new C();
c.method(b);
}
}
- 接口名作为返回值类型
abstract class D{
public abstract void study();
}
class E extends D{
@Override
public void study() {
System.out.println("你是四月早天里的呢喃,如沐清风");
}
}
class F{
public E method(E e){
e.study();
return e;
}
}
public class 接口名作为返回值类型 {
public static void main(String[] args) {
E e = new E();
F f = new F();
f.method(e);
}
}