小编mab*_*aba的帖子

如何使用更通用的IEnumerable <T>?

我的代码如下:

PropertyInfo p_info_keys = obj.GetType().GetProperty("Keys");
IEnumerable<string> keys = (IEnumerable<string>)p_info_keys.GetValue(obj, null);

foreach (string key in keys)
{
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

问题是这一行:

IEnumerable<string> keys = (IEnumerable<string>)p_info_keys.GetValue(obj, null);
Run Code Online (Sandbox Code Playgroud)

因为它可以是:

IEnumerable<decimal> keys = (IEnumerable<decimal>)p_info_keys.GetValue(obj, null);
Run Code Online (Sandbox Code Playgroud)

我试过用这个:

IEnumerable<object> keys = (IEnumerable<object>)p_info_keys.GetValue(obj, null);
Run Code Online (Sandbox Code Playgroud)

但是,当然,它不起作用.

那么,我如何使用可以接受字符串和小数的更通用的结构?

先感谢您.

c# generics

4
推荐指数
1
解决办法
199
查看次数

如何在java中解析文件名?

我有一个java文件路径

/opt/test/myfolder/myinsidefolder/myfile.jar

我想替换的文件路径到这里的根路径将保持不变,但想从更改文件名myfile.jarTest.xml

/opt/test/myfolder/myinsidefolder/Test.xml

我怎么能在java中做任何帮助?

java

3
推荐指数
2
解决办法
5691
查看次数

将Object与无序整数对作为变量进行比较时需要正确的hashCode

我上课了

final class BuildingPair {

    int mBA;
    int mBB;

    public BuildingPair(int pBuildingA,int pBuildingB) {
        mBA = pBuildingA;
        mBB = pBuildingB;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + mBA;
        result = prime * result + mBB;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BuildingPair other = (BuildingPair) …
Run Code Online (Sandbox Code Playgroud)

java comparison equals hashcode

3
推荐指数
1
解决办法
1547
查看次数

Groovy强制转换表现不一致

我今天碰到了一大堆代码,等同于:

[["asdf"]].each {String str -> println str}

println (["asdf"] as String)
Run Code Online (Sandbox Code Playgroud)

把它放到groovy console(groovysh)中,你就会看到这个:

asdf

[asdf]
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么输出有差异?

groovy

3
推荐指数
1
解决办法
107
查看次数

每次调用new运算符时,java都会创建一个新对象

在java中,当我们调用a时new Constructor(),每次都会创建一个新对象,即; 分配新内存或假设已经为没有任何引用的类创建了许多对象.

因此,java可以返回标记为取消分配的对象,或者每次调用新的构造函数()时java都会创建一个新对象.

我提出这个问题的基本意图是,如果发生这样的事情,那么可以改善性能,因为创建新内存和销毁未经修复的对象的成本将会降低.

java

3
推荐指数
1
解决办法
1215
查看次数

如何将spring xml从3.0转换为3.1使用bean:profile

我有一个像3.0这样的xml:

        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.internal.url}" />
            <property name="username" value="${jdbc.internal.username}" />        
            <property name="password" value="${jdbc.internal.password}"/>
        </bean>
Run Code Online (Sandbox Code Playgroud)

我想在使用时将其转换为3.1 beans:profile但是,当我尝试将其更改为:

        <beans profile="dev">
          <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="${jdbc.driverClassName}" />
              <property name="url" value="${jdbc.internal.url}" />
              <property name="username" value="${jdbc.internal.username}" />        
              <property name="password" value="${jdbc.internal.password}"/>
        </bean>
        </beans>
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}'
Run Code Online (Sandbox Code Playgroud)

我如何使用它,beans:profile以便仅在活动配置文件时调用此特定bean定义dev

更新 我的bean定义是:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-3

3
推荐指数
1
解决办法
1169
查看次数

Java,从方法返回一个值

我在这里做了一段简单的java代码,它是一个被调用来将华氏温度转换为摄氏温度的方法.然而,当我运行它时,输出始终是0.0.

我不能让它输出正确的计算.可能有一些我忘记做的傻事,有人可以帮帮我吗?

public class ExampleQ {

    public static void main(String[] args) {

        System.out.println(Celsius(50));
    }

    public static double Celsius(double F)
    {
        return (5 / 9) * (F - 32);
    }
}
Run Code Online (Sandbox Code Playgroud)

java methods return return-value

3
推荐指数
2
解决办法
626
查看次数

如何在java中解析对静态方法的调用?

如果在编译时解析静态方法,那么对象实例如何能够调用静态方法?

class StaticCall
{
    public static void main(String[] args)
    {
        String arr[]={"Class StaticCall","calls static method of class MyMainClass"};

        MyMainClass h=new MyMainClass();
        h.main(arr);         //How is an instance able to call a static method?
        System.out.println("this is StaticCall main");  
    }   

}


class MyMainClass 
{
    public static void main(String[] args){
        System.out.println(args[0]+" "+ args[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

运行StaticCall类后,输出为

StaticCall类调用MyMainClass类的静态方法

这是StaticCall的主要内容

由于静态字段和方法属于Class对象,实例如何能够调用静态方法?此外,何时创建了Class对象,它是否首次访问其中的任何字段或方法?

java static

2
推荐指数
1
解决办法
391
查看次数

For循环后,Java Scanner不接受输入

我目前正在制作一个程序,要求输入两个团队的名称和分数.当我请求输入名称和第一组的9分时,扫描仪接受输入就好了.但是,在for循环之后,扫描程序不接受第二个团队名称的输入.这不是整个程序,但我已经包含了所有代码,直到它给我带来麻烦.我怀疑它可能与for循环有关,因为当我将它放在for循环之前,team2接受用户输入就好了.

import java.util.Scanner;
public class sportsGame{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String team1;
        String team2;
        int team1Scores[] = new int[9]
        int team1Total = 0;
        int team2Scores[] = new int[9];
        int team2Total = 0;

        System.out.print("Pick a name for the first team: ");
        team1 = input.nextLine();

        System.out.print("Enter a score for each of the 9 innings for the "
                + team1 + " separated by spaces: ");
        for(int i = 0; i < team1Scores.length; i++){
            team1Scores[i] = input.nextInt(); …
Run Code Online (Sandbox Code Playgroud)

java for-loop input

1
推荐指数
1
解决办法
2125
查看次数

如何调用匿名内部类的方法?

我理解这段代码不合法​​:

class Popcorn {
    public void pop() {
        System.out.println("popcorn");
    }
}

class Food {
    Popcorn p = new Popcorn() {
        public void sizzle() {
            System.out.println("anonymous sizzling popcorn");
        }
        public void pop() {
            System.out.println("anonymous popcorn");
        }
    };
    public void popIt() {
        p.pop(); // OK, Popcorn has a pop() method
        p.sizzle(); // Not Legal! Popcorn does not have sizzle()
    }
}
Run Code Online (Sandbox Code Playgroud)

那么什么是一种调用嘶嘶声方法的方法呢?

java anonymous-inner-class inner-classes

1
推荐指数
1
解决办法
183
查看次数