java初学--class extends

本文通过具体的Java代码示例介绍了面向对象编程的基本概念,包括类的定义、继承、多态等核心特性,并展示了如何使用这些特性来实现简单的交互逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Person.java

public class Person 
{
	int age;
	String name;
	String school;
	Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	Person(String string, int age, String school) 
	{
		this.name=name;
		this.age=age;
		this.school=school;
	}
	void sayHello(Student s) 
	{
		System.out.println("My school is "+school);
	}
    void sayHello()
    {
    	System.out.println("wpx");
    }

}

Student.java

public class Student extends Person
{
	String school;
	int score;
	void sayHello(Student another)
	{
		System.out.println("Hi!");
		if(school==another.school )
			System.out.println(" Shoolmates");
	}
	boolean isGoodStudent()
	{
		return score>=90;
	}
	void testThisSuper()
	{
		int a;
		a=age;
		a=this.age;
		a=super.age;	
	}
	void sayHello()
	{
		super.sayHello();
		System.out.println("My school is "+school);
	}
	Student(String name,int age,String school)
	{
		super(name,age);
		this.school=school;
	}
//	Student()();
	public static void main(String []args)
	{
		Person p=new Person("Liming",50);
		Student s=new Student("Wangqiang",20,"PKU");
		Person p2=new Student("Zhangyi",18,"THU");
//		Student s2=(Student)p2;//强制转换
//		Student s3=(Student)p;
		
		p.sayHello(s);
		Person []manypeople = new Person[100];
		manypeople[0] = new Person("Li",18);
		manypeople[1] = new Person("Wang",18,"PKU");
	}
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值