| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1031 人关注过本帖
标题:java函数问题
只看楼主 加入收藏
小文文化
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-9-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
java函数问题
我有以下一个java文件:
package testjava1;

public class testjava2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub           
           print("This is a two java");
    }
   
    public static void print(String value)
    {
          System.out.println(value);
    }

}
我想把print这个函数单独写成一个独立文件上,在调用这个函数是只要引入一个独立文件,然后像print("This is a two java");这样调用
搜索更多相关主题的帖子: java 函数 文件 public print 
2017-09-05 15:11
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
收藏
得分:20 
java是面向对象语言,属性和方法不能脱离某个类或者对象单独存在
你说的这个问题,可以写一个工具类,如PrintUtil,然后把你的print方法放到里面
其他的类可以通过PrintUtil.print("....")调用该方法(必须是静态方法,否则需要通过对象调用)
2017-09-05 19:58
小文文化
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-9-5
收藏
得分:0 
我自己也想到另外一种方法是可以写一个工具类,如common,然后把你的print方法放到里面,这个方式同calix说的前面方法是一样的,但我想的方法后面就同calix说的后面有些不同了,我的方法是我在有main方法的类中继承common类,那么在这个类中直接调用方法就可以了,就像以下两个程序:
首先是common类:
package javatest1;

public class common {
   
    public static <E> void print(E value)
    {
        System.out.println(value);
    }


}
然后是继承common类有main方法的主类:
package javatest1;

public class javatest1 extends common{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
           print("Hello World!");
           print(100);
           print(0b100);
           print(0100);
           print(0x100);
           print(true);
           print(false);
           print(null);
    }

}
我主要是想是定义一个通用的类放一些通用的方法如System.out.println("Hello World");和System.out.println("This is a java program");类似的输出语句每次想输出一句语句都要写System.out.println这句有点麻烦,我想一次性定义一个通用的函数如print把每次输出语句的工作量减少,就想到以上的程序,还要一个问题我想问:上面的common类中public static <E> void print(E value)(即print函数的定义),我明白参数value前面中的E是泛型(即调用这个函数的语句在传入参数任意类型都可以),但void前面的<E>自己觉得是多余的,但把void前面的<E>删除程序就报
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E
    The method print(E) from the type common refers to the missing type E

    at javatest1.javatest1.main(javatest1.java:10)
这个错误,那么void前面的<E>起的作用是什么?
2017-09-06 08:55
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
收藏
得分:0 
java是单继承的,为了一个简单的方法就浪费掉唯一的继承,不合算,会降低代码的拓展和灵活性
<E>用来声明E是一个泛型,就像变量使用前要声明一样,你也可以将<E>放到类名上,比如Common<E>,下面就可以直接使用了
2017-09-06 20:12
快速回复:java函数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018300 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved