【JAVA学习路-think in java】p114:如何定制工具库(尤其是print方法)

package zeng;
import java.io.*;

// NO PUBLIC main here

public class Print {
	public static void println(Object obj){
		System.out.println(obj);
	}
	public static void println(){
		System.out.println();
	}
	public static void print(Object obj){
		System.out.println(obj);
	}
	public static void print(){
		System.out.println();
	}
	//The new Java SE5 printf() (from C)
	public static PrintStream printf(String format, Object... args){
		return System.out.printf(format, args);
	}
}
//-------------- eg 1  ---------------

package pkg;
import static zeng.Print.*;       
// import static 语句一定要包含到类内的 static 方法,这样 static 方法是可见的,而 Print 类是不可见的,

//import zeng.Print;

public class p114 {
	
	public static void main(String[] args) {
		
		print("Avaliable from now on!");       
		// 这里如果多此一举使用 Print.会报错,因为不认识 Print 类,需要 import **.**.Print;
		print(100);
		print(100L);
		print(3.1415926);
		
		//test
		//Print p=new Print();//The method print(String) is undefined for the type p114
		//p.print("Can this be seen?,NO, It can't!");
		
		//test 2
		//Print.print("can this be seen?,yes too");//The method print(String) is undefined for the type p114
		
	}
	
}

OUTPUT:

Avaliable from now on!
100
100
3.1415926
 

//-------------- eg 2  ---------------
package pkg;
import zeng.Print;

public class p114 {
	
	public static void main(String[] args) {
		
		/*
        print("Avaliable from now on!"); 
        print(100);
        print(100L);
        print(3.1415926);
		 */
		//The method print(String) is undefined for the type p114
		
		
		
		//test
		Print p=new Print();
		p.print("Can this be seen?,yes you can");/*
        The static method print(Object) from the type Print should be accessed in a static way */
		
		//test 2
		Print.print("can this be seen?,yes too");
	}
	
}

OUTPUT:

Can this be seen?,yes you can
can this be seen?,yes too
 

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值