对于以下程序,为什么要调用具有int和String参数的方法而不是long和Object?
想知道为什么编译器选择int一遍long又String一遍的Object参数。
注意:这是在采访中问到的。
public class MethodOverloadingTest {
public static void add(int n, int m){
System.out.println("Int method");
System.out.println(n+m);
}
public static void add(long n, long m){
System.out.println("Long method");
System.out.println(n+m);
}
public static void method(String st){
System.out.println("from String method");
}
public static void method(Object obj){
System.out.println("from Object method");
}
/**
* @param args
*/
public static void main(String[] args) {
add(2,3);
method(null);
}
}
Run Code Online (Sandbox Code Playgroud) 我是C++的新手(我是C程序员),所以如果这看起来像一个愚蠢的问题,我道歉.
当我运行此程序时,我收到以下错误消息:
错误C2661:'Student :: Student':没有重载函数需要2个参数
我评论了错误发生的位置(2个实例).谢谢.
//Definition.cpp
#include "Student.h"
Student::Student(string initName, double initGPA) //error here and in main.cpp
{
name = initName;
GPA = initGPA;
}
string Student::getName()
{
return name;
}
double Student::getGPA()
{
return GPA;
}
void Student::printInfo()
{
cout << name << " is a student with GPA: " << GPA << endl;
}
//student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
private:
string name;
double GPA;
public:
string getName();
double getGPA();
void setGPA(double …Run Code Online (Sandbox Code Playgroud) 我operator []在使用C++ 重载矩阵时遇到了一些问题.让我说Class Matrix我有一个2X2.matrix m[2][2]如果我想从数组中调用一个元素m[1][1]而不是代码应该是什么样子object.m[1][1]?我想标题应该是类似int operator[] (const int)但我不知道如何建立...如果有人可以提前帮助我.
什么是最容易为方法编写重载的方法,我不关心用户输入参数的顺序以及类型总是不同的方式?
例如:
public void DoSomething(String str, int i, bool b, DateTime d)
{
//do something...
}
Run Code Online (Sandbox Code Playgroud)
现在我想有可能以任何可能的方式调用该方法,例如:
DoSomething(1, DateTime.Now, "HelloWorld", false);
DoSomething(DateTime.Now, 1, "HelloWorld", false);
DoSomething("HelloWorld", DateTime.Now, 1, false);
DoSomething(false, DateTime.Now, "HelloWorld", 1);
//and so on...
Run Code Online (Sandbox Code Playgroud)
除了一遍又一遍地复制方法并重新排列参数之外,真的没有办法吗?
当你为参数指定默认值并且在调用方法时需要指定名称或者设置默认值时,我特别认为它很烦人.
理想情况下,我想实现类似的东西 var queryResult = ~( () => function(arg) );
我写了一个自动化框架,我厌倦了为它编写长行代码.它使用查询来搜索UI和其他数据源.所以我想,由于这个项目与源代码是分开的,我可以通过操作员来解决问题.以下编译很好,但我收到一个错误:
不能将运算符'〜'应用于lambda表达式的操作数
public struct Query<T>
{
public static Query<T> operator ~(Query<T> func)
{
return Poll.IdleFor(() => func, 10000);
}
}
Run Code Online (Sandbox Code Playgroud)
在正在运行的代码的另一部分中,我有一个方法签名,如下所示:
static T IdleFor<T>(Func<T> arg, long waitTimeInMilliseconds)<T>(Func<T> arg, long milliseconds)
我通过它来称呼它 MyStaticClass.IdleFor( () => myFunction(val), 10000)
我在这里错过了什么?
我在接受采访时被问到这个问题.
任何人都可以解释它.
public class A{
public void show(List <String>list1,List<Integer>lists2){...}
public void show(List <Integer>list1,List<String>lists2){...}
}
public class B{
public static void main(..){
A a=new A();
List<String> list1;
List<Integer>lists2;
a.show(list1,lists2);
}
}
Run Code Online (Sandbox Code Playgroud)
我说2个函数会被调用..但是当我在neatbeans中worte它给出了同名函数的错误被称为... ??? 为什么这里没有重载概念?
我有一个基本的抽象类Base.
class Base
{
protected:
string m_Name;
public:
virtual string Name() { return m_Name; }
virtual string Type() = 0;
virtual bool isEqual(Base* rhs) = 0 ;
//virtual ostream& operator<< (ostream& out) const;
};
Run Code Online (Sandbox Code Playgroud)
我想重载operator <<显示继承的对象Base.我不能使用void print()函数,因为这些继承的对象Base也有一些只能显示的对象operator <<.
我怎么能超负荷operator <<?
让我们看看以下函数重载声明
void funA(int);
void funA(float);
Run Code Online (Sandbox Code Playgroud)
然后我们调用这样的函数:
funA(1); // this will be ok.
Run Code Online (Sandbox Code Playgroud)
然而,
funA(1.333) // this will not ok..
Run Code Online (Sandbox Code Playgroud)
对于编译器,funA(int)和funA(float)将是不明确的..编译器将1.333值转换为整数(1)..虽然假设它是一个浮点值会更合适..
我正在使用g ++(GCC)4.8.2为什么编译器不会调用funA(float)呢?但是以下工作..
funA(static_cast<float>(1.333))
Run Code Online (Sandbox Code Playgroud) 假设我想定义一个带有int和string的新类型.我知道要这样定义:
data Comp = Comp Int String
但这种表示法的问题是,当我有这种类型的实例时,实例的形式为:
Comp 45 "heloWorld"
我不希望这样,我想要数据类型的这些"参数"(更像是记录的条目),我希望它们以字符分隔,例如| 或#而不是空格.我不希望那个角色做任何事情,这只是我想要使用的符号.在这种情况下,
45#"hii"
是我想要的而不是
45 "hii".为了做到这一点,我如何调整数据类型的定义?我打算为此数据类型重载运算符,例如:
45#"kkk" + 128#"a"应该是一个有效的操作.
假设我有2个字段的类:x和y,类型double.是否可以定义2个构造函数,因此constructor1将创建对象,将其x属性设置为构造函数中的哪个参数告诉y默认值,构造函数2反之亦然?
public class Test {
private int x;
private int y;
public Test(int x) {
this.x = x;
}
public Test(int y) {
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样的东西,我知道它因为超载规则而无法工作