⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleclassdemo10.java

📁 JAVA程序设计课程中各章节的程序实例。
💻 JAVA
字号:
/**
*A demonstration program of the instanceof operator
*In object-oriented programming language upcasting is always legal
* it implies that a child class object can be referenced through a parent reference variable
* If the objects are passed around say in a function using reference to their parents
* the instanceof operator provides a way to differentiate between them as is shown in the 
* program below
*It's a classic example!

*/

class Student
{
	public void print()
	{
		System.out.println("I'm a student");
	}
}

class Freshman extends Student
{
	public void print()
	{
		System.out.println("I'm a first year student");
	}
}

class Sophomore extends Student
{
	public void print()
	{
		System.out.println("I'm a second year student");
	}
}

public class simpleClassDemo10
{
	public void identify(Student s)
	{
		if(s instanceof Freshman)
		{
			s.print();
			
			System.out.println("Instance of the Freshman");
		}
			
		if(s instanceof Sophomore)
		{
			s.print();
			
			System.out.println("Instance of the Sophomore");
		}
	}

	public static void main(String args[])
	{
		Freshman obj1 = new Freshman();
		Sophomore obj2 = new Sophomore();
		
		simpleClassDemo10 obj = new simpleClassDemo10();
		
		obj.identify(obj1);
		obj.identify(obj2);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -