对于下面的代码,我确实理解调用方法不明确要调用哪个重载方法是有意义的,但是我无法理解此处如何准确检查参数匹配。
public class Test{
public void m1(float i,int j){
System.out.println("float method ");
}
public void m1(int i, float j){
System.out.println("int method ");
}
public static void main(String[] args) {
Test a=new Test();
a.m1(10,10); // The method m1(float, int) is ambiguous for the type Test
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个具有接口I和类A并B实现它的 Java 程序。我还有另一个类实现C ,它有 2 个带有一个参数的方法:一个接受 A 的对象,另一个接受 B 的对象。
interface I {}
static class A implements I {}
static class B implements I {}
interface C {
String map(A a);
String map(B b);
}
public static void main(String[] args) {
// Injected at runtime;
C mapper = getInstance();
I a = new A();
I b = new B();
// This fail at compilation
String result = mapper.map(a);
}
private static C getInstance() …Run Code Online (Sandbox Code Playgroud) 我试图使用一个文件在命令窗口中创建一个菜单.用户从这些菜单选项中进行选择.系统会提示他们输入一个数字.该数字传递给两个重载方法,这些方法确定数字是整数还是浮点数.计算完成后,结果将打印到屏幕上,菜单重新出现.这是我的两个文件中的代码.
MyMathOpsTest类:
import java.util.Scanner; // import Scanner class
public class MyMathOpsTest
{
//method to pause until a key is pressed
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}//end pause
public static void main( String args[] )
{
//variables to capture keyboard input
Scanner keyBd = new Scanner( System.in );
char selection;
//int selection;
do{//display menu
System.out.println( "1. Square a Number");
System.out.println( "2. Cube a Number");
System.out.println( "3. …Run Code Online (Sandbox Code Playgroud) 我在Start()时收到"No overload take 0 args"的错误; 在我的主要方法中.我不知道如何解决它,我已经四处搜索,找不到任何东西.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void main(string[] args)
{
Start();
}
public static string Start(string move)
{
Console.Write("");
string gameType = Console.ReadLine();
if (gameType == "s")
{
Console.Write("");
begin:
Console.Write("\nEnter your move: ");
move = Console.ReadLine();
switch (move)
{
case "r":
Console.Write("s");
Console.ReadLine();
break;
case "s":
Console.Write("");
Console.ReadLine();
break;
case "f":
Console.Write("");
Console.ReadLine();
break;
default:
Console.Write("\nInvalid move, try again\n\n");
goto begin;
}
Console.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我想要做的是如下:
(some data type say x) function_name((same data type x mentioned earlier) variable_name)
{
/* Function body */
}
int main()
{
(data type x mentioned above) var;
function_name(var);
.....
}
Run Code Online (Sandbox Code Playgroud)
比如我
int function_name(int x)
int main()
{
int y;
function_name(y);
....}
Run Code Online (Sandbox Code Playgroud)
在main()中,我通过上面的函数传递一个int变量现在我想要一个与上面的函数完全相同的函数的另一个函数,但只是参数的数据类型和返回类型不同,比如说
unsigned long long FUNCTION_NAME_SAME_DEFINITION_AS_function_name(unsigned long long x)
Run Code Online (Sandbox Code Playgroud)
有没有办法可以合并上面两个函数,而不使用函数重载?意味着函数检测到哪个数据类型的变量通过它?如果我传递类型1的参数,则返回数据类型1的值,如果我传递类型2的参数,则返回数据类型2的值.
我有以下代码:
public interface Block {
public double[] getOutput();
public double[] getOutput(double[] inputs);
}
public class Dataset implements Block{
public double[] getOutput(){
return(new double[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用netbeans IDE,它会产生以下消息:
Dataset is not abstract and does not override abstract method getOutput(double[]) in Block
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会发生这种情况..任何帮助将不胜感激
谢谢
在ViewController类中,我有两个mapView函数:
第一个mapView函数处理叠加(如地图上的线条,形状等)
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
Run Code Online (Sandbox Code Playgroud)
第二个mapView函数处理地图上的注释和引脚
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
Run Code Online (Sandbox Code Playgroud)
来自C/Python背景,我不明白你如何能够有两个命名相同的函数,并且它们不会相互覆盖.这个过程背后的想法是什么?
通过this()调用重载构造函数可能有用的一个原因是它可以防止不必要的代码重复.在许多情况下,减少重复代码会减少加载类所需的时间,因为对象代码通常较小.这对于通过因特网提供的程序尤为重要,因为加载时间是一个问题.
但是,你需要小心.调用this()的构造函数的执行速度比包含所有内联初始化代码的构造函数要慢一些.这是因为调用第二个构造函数时使用的调用和返回机制会增加开销.如果你的类只用于创建少数几个对象,或者调用this()的类中的构造函数很少被使用,那么这种运行时性能的降低可能是微不足道的.
加载课程的时间如何变小?什么应该是在构造函数中使用它和使用内联代码之间的权衡点?
我试图从PHP移植一些代码,基本归结为属性重载.也就是说,如果你试图获取或设置一个实际上没有被定义为类的一部分的类属性,它会将该信息发送给一个几乎可以用它做任何事情的函数.(在这种情况下,我想在放弃之前在类中搜索关联数组.)
但是,Perl与PHP有很大的不同,因为类已经是哈希.有没有什么方法可以将一些等效的__get()和应用__set()到Perl"类"中,它仍将完全封装在该包中,对于试图实际获取或设置属性的任何内容都是透明的?
编辑:解释这个的最好方法可能是向您显示代码,显示输出,然后显示我想要输出的内容.
package AccessTest;
my $test = new Sammich; #"improper" style, don't care, not part of the question.
say 'bacon is: ' . $test->{'bacon'};
say 'cheese is: ' . $test->{'cheese'};
for (keys $test->{'moreProperties'}) {
say "$_ => " . $test->{'moreProperties'}{$_};
}
say 'invalid is: ' . $test->{'invalid'};
say 'Setting invalid.';
$test->{'invalid'} = 'true';
say 'invalid is now: ' . $test->{'invalid'};
for (keys $test->{'moreProperties'}) {
say "$_ => " …Run Code Online (Sandbox Code Playgroud) #include问题
class widget { };
class fubar : public widget { // 1
void value_parameter(widget); // 2
void ref_parameter(widget &); // 3
void ptr_parameter(widget *); // 4
virtual void value_parameter(widget); // 5
virtual void ref_parameter(widget &); // 6
virtual void ptr_parameter(widget *); // 7
widget value_return(); // 8
widget & ref_return(); // 9
widget * ptr_return(); // 10
widget instance_value_member; // 11
widget & instance_ref_member; // 12
widget * instance_ptr_member; // 13
static widget static_value_member; // 14
static widget & …Run Code Online (Sandbox Code Playgroud) overloading ×10
java ×5
c++ ×2
oop ×2
c# ×1
class ×1
constructor ×1
definition ×1
function ×1
interface ×1
ios ×1
java-17 ×1
performance ×1
perl ×1
swift ×1
swift3 ×1
this ×1