package b;
//本程序说明字符串直接赋值只开辟一块堆内存,而new创建的实例需要分别开辟堆内存
public class B {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String a="hello";
		String b="hello";
		String c="hello";
		String A=new String("WORLD");
		String B=new String("WORLD");
		String C=new String("WORLD");
		System.out.println((a==b)+"、"+(a==c)+"、"+(b==c)); //判断String对象地址是否相同
		System.out.println((A==B)+"、"+(A==C)+"、"+(B==C));	
	}
}

运行结果:

在线运行java测试代码_System

package b;
class Dem
{
	int temp=3;
}
class Demo
{	
	String temp="hello";
}
public class B {

	public static void main(String[] args)throws Exception {
		// 说明类的传参属于引用传递
		System.out.println("-------------------------------");
		Dem a=new Dem();
		System.out.println("fun() is not used: "+a.temp);
		fun(a); 
		System.out.println("fun() is used: "+a.temp);
		System.out.println("-------------------------------");
		// 字符串类比较特殊,字符串一旦实例后,内容不可改变
		String str1="hello"; 
		System.out.println("fun() is not used: "+str1);
		fun(str1); 
		System.out.println("fun() is used: "+str1);
		System.out.println("-------------------------------");
		//将字符串封装在类中,实现字符串的改变
		Demo d1=new Demo();		
		System.out.println("fun() is not used: "+d1.temp);
		fun(d1);
		System.out.println("fun() is used: "+d1.temp);
		System.out.println("-------------------------------");
	}	
	public static void fun(Dem a)
	{
		a.temp=999;
	}
	public static void fun(String str2)
	{
		str2="MLDN";
	}
	public static void fun(Demo d2)
	{
		d2.temp="MLDN";
	}
}

运行结果:

在线运行java测试代码_在线运行java测试代码_02

//多态测试小程序
package b;
class A
{
	public void fun1()
	{
		System.out.println("A-->fun1()");
	}
	public void fun()
	{
		this.fun1();
	}
}
class C extends A
{
	public void fun1()
	{
		System.out.println("C-->fun1()");
	}
	public void fun3()
	{
		System.out.println("C-->fun3()");
	}
}
public class B {

	public static void main(String[] args){
		// 说明类的传参属于引用传递
		C c=new C();
		A a=c;
		a.fun1(); //调用的是子类覆写的函数
	}		
}

运行结果:


在线运行java测试代码_子类_03

//多态测试小程序-1
package b;
class A
{
	public void fun1()
	{
		System.out.println("A-->fun1()");
	}
	public void fun2()
	{
		this.fun1();
	}
}
class C extends A
{
	public void fun1()
	{
		System.out.println("C-->fun1()");
	}
	public void fun3()
	{
		System.out.println("C-->fun3()");
	}
}
public class B {

	public static void main(String[] args){		
		A a=new C();
		C c=(C)a;
		c.fun1(); //调用子类覆写的函数
		c.fun2(); //调用父类fun2函数,fun2又调用子类覆写的fun1函数
		c.fun3(); //调用子类函数
	}		
}

运行结果:

在线运行java测试代码_System_04

//模板设计模式P206
package b;
abstract class Person
{
	private String name;
	private int age;
	//如果自己写了构造函数,java将不会为类创建默认构造函数public Person()
	public Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
	public void say()
	{
		//根据多态性,调用的是子类覆写的getContent()函数
		System.out.println(this.getContent());
	}
	//定义抽象函数,子类必须覆写
	public abstract String getContent();
}
class Student extends Person
{
	private float score;
	//java规定,当构造子类时,先构造父类,再构造子类
	//所有super(name,age)是必须的,否则编译出错
	public Student(String name,int age,float score)
	{
		super(name,age);
		this.score=score;
	}
	public String getContent()
	{
		return "Student info-->name: "+super.getName()+
				" age: "+super.getAge()+" score: "+this.score;
	}
}
class Worker extends Person
{
	private float salary;
	public Worker(String name,int age,float salary)
	{
		super(name,age);
		this.salary=salary;
	}
	public String getContent()
	{
		return "Worker info-->name: "+super.getName()+
				" age: "+super.getAge()+" salary: "+this.salary;
	}
}
public class B {

	public static void main(String[] args){		
		Person per1=new Student("Tom",20,99f);
		Person per2=new Worker("David",35,6000f);
		per1.say();
		per2.say();
	}		
}

运行结果:

在线运行java测试代码_在线运行java测试代码_05

//制定USB接口标准
package b;
interface USB
{
	public void start();
	public void stop();
}
class Computer
{
	public static void plugin(USB usb)
	{
		usb.start();
		System.out.println("======USB设备工作======");
		usb.stop();
	}
}
class Flash implements USB       //U盘
{
	public void start()
	{
		System.out.println("U盘开始工作。");
	}
	public void stop()
	{
		System.out.println("U盘停止工作。");
	}
}
class Print implements USB          //打印机
{
	public void start()
	{
		System.out.println("打印机开始工作。");
	}
	public void stop()
	{
		System.out.println("打印机停止工作。");
	}
}
public class B {
	public static void main(String[] args){		
		Computer.plugin(new Flash());
		Computer.plugin(new Print());
	}		
}

运行结果:

在线运行java测试代码_覆写_06

//工厂模式
package b;
interface Fruit
{
	public void eat();
}
class Orange implements Fruit
{
	public void eat()
	{
		System.out.println("**ear orange");
	}
}
class Apple implements Fruit
{
	public void eat()
	{
		System.out.println("**eat apple");
	}
}
//工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来
class Factory
{
	public static Fruit getInstance(String className)
	{
		Fruit f=null;
		if("apple".equals(className))
		{
			f=new Apple();
		}
		if("orange".equals(className))
		{
			f=new Orange();
		}
		return f;
	}
}
public class B {
	public static void main(String[] args){
		Fruit f=null;
		//"apple"可以通过args传入参数
		f=Factory.getInstance("apple");
		f.eat();
	}
}

运行结果:

在线运行java测试代码_覆写_07

//代理设计模式
package b;
interface Network
{
	public void  browse();
}
class Real implements Network
{
	public void browse()
	{
		System.out.println("上网浏览信息");  //5.调用此browse()
	}
}
class Proxy implements Network
{
	private Network network;
	public Proxy(Network network)
	{
		this.network=network;   //3.this.network=new Real()
	}
	public void check()
	{
		System.out.println("检查用户是否合法");
	}
	public void browse()
	{
		this.check(); //2.调用check()
		this.network.browse(); //4.调用this.newwork中的browse()
	}
}
public class B {
	public static void main(String[] args){
		Network net=null;
		net=new Proxy(new Real());  
		net.browse();  //1:调用Proxy中browse()
	}
}

运行结果:

在线运行java测试代码_子类_08

//适配器设计模式
package b;
interface Window
{
	public void open();
	public void close();
	public void activated();
	public void iconified();
	public void deiconified();	
}
abstract class WindowAdapter implements Window
{ //抽象类(适配器)要覆写接口中所有函数,但为空函数体
	public void activated(){}
	public void close(){}
	public void deiconified(){}
	public void iconified(){}
	public void open(){}
}
class WindowImpl extends WindowAdapter
{//通过适配器,达到只覆写部分接口函数
	public void open()
	{
		System.out.println("窗口打开。");
	}
	public void close()
	{
		System.out.println("窗口关闭。");
	}
}

public class B {
	public static void main(String[] args){
		Window win=new WindowImpl();
		win.open();
		win.close();
	}
}

运行结果:

在线运行java测试代码_System_09

//Object类toString()函数的使用
package b;
class Demo
{
	
}
class Person
{
	private String name;
	private int age;
	public Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	public String toString()
	{
		return "name: "+this.name+" age: "+this.age;
	}
}
public class B {
	public static void main(String[] args){
		Demo d=new Demo();
		//在输出类信息时默认调用了toString函数,当在子类覆写了toString,
		//则调用子类toString函数,若没有覆写,则调用Object中的toString函数
		System.out.println("---------------------------");
		System.out.println(d);
		System.out.println(d.toString());
		System.out.println("---------------------------");
		Person per=new Person("Tom",25);
		System.out.println(per);
		System.out.println(per.toString());
		System.out.println("---------------------------");
	}
}

运行结果:

在线运行java测试代码_覆写_10

//Object类equals函数是比较地址,等价于==
package b;
class Demo
{
	
}

public class B {
	public static void main(String[] args){
		Demo d=new Demo();
		Demo d1=new Demo();
		Demo d2=d;
		System.out.println(d.equals(d1)+"、"+d.equals(d2)+"、"+(d==d1)+"、"+(d==d2));	
	}
}

运行结果:false、true、false、true

//使用Object类接收数组
package b;
public class B {
	public static void main(String[] args){
		int temp[]={1,3,5,7,9};
		Object obj=temp;
		print(obj);
	}
	public static void print(Object o)
	{
		if(o instanceof int[])
		{
			int x[]=(int[])o;
			for(int i=0;i<x.length;i++)
				System.out.print(x[i]+"\t");
			System.out.println();
			for(int xx:(int[])o)     //使用foreach
				System.out.print(xx+"\t");
		}
	}
}

运行结果:


1 

3  5  7  9


1  3  5 7  9

//匿名内部类 code-1
package b;
interface A
{
	public void printInfo();
}
class C implements A
{
	public void printInfo()
	{
		System.out.println("Hello World!!!");
	}
}
class X
{
	public void fun1()
	{
		this.fun2(new C());
	}
	public void fun2(A a)
	{
		a.printInfo();
	}
}
public class B {
	public static void main(String[] args){
		new X().fun1();
	}
}

运行结果:Hello World!!!

问题:如果以上程序,接口的实现类(即类C)仅使用一次,还有必要单独定义一个类C吗?使用匿名内部类解决以上问题。

//匿名内部类 code-2
package b;
interface A
{
	public void printInfo();
}
class X
{
	public void fun1()
	{
		this.fun2(new A() 
		{
			public void printInfo()
			{
				System.out.println("Hello World!!!");
			}
		});
	}
	public void fun2(A a)
	{
		a.printInfo();
	}
}
public class B {
	public static void main(String[] args){
		new X().fun1();
	}
}

运行结果:Hello World!!!


解析:直接实例化接口对象,代码为:new A() { },由于

接口本身是不能直接进行实例化的,所以在接口实例化后要有一个大括号,在其中编写具体的实现方法。


package b;
//线程的启动要使用start函数,start函数会执行run方法
//主方法是一个线程,线程名为main
class MyThread implements Runnable
{
	public void run()
	{
		for(int i=0;i<3;i++)
			System.out.println(Thread.currentThread().getName()
					+"运行,i="+i);
	}
}
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread my=new MyThread();
		new Thread(my,"线程").start(); //启动了一个线程,和main线程并行运行,会调用run方法
		my.run();  //普通的函数调用,在main线程中执行
		my.run();
	}
}

运行结果:

在线运行java测试代码_覆写_11