运行时异常 - java.lang.ClassCastingException
......
Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;
Run Code Online (Sandbox Code Playgroud)
我知道降级不安全,但为什么会这样呢?我也试图转换ArrayList
到String
以Integer
对int
,但我得到了同样的错误.
这是我编写的一段代码,用于查看向下转换期间的行为.
#include <iostream>
using namespace std;
class base {
public :
void function()
{
cout << "\nInside class Base";
}
};
class derived : public base {
public :
void function()
{
cout << "\nInside class Derived.";
}
};
int main()
{
base * b1 = new base();
base * b2 = new derived();
derived * b3 = (derived*)b1 ;
b1 -> function();
b2 -> function();
b3 -> function(); // print statement 3
static_cast<derived*>(b2) -> function();
static_cast<derived*>(b1) -> function(); // …
Run Code Online (Sandbox Code Playgroud) 我正在尝试投射(任何)?值为整数。我正在从 Firebase 检索信息,并且想通过其他内容添加该号码。这就是为什么(任何)?值需要是一个整数。我有这个:
let snapshotValues = snapshot.value as? NSDictionary
let gamesWon = snapshotValues!.value(forKey: "GamesWon")
let gamesLost = snapshotValues!.value(forKey: "GamesLost")
let totalgamesPlayedByUser = gamesWon + gamesLost
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误,两个 Any? 对象不能添加在一起。我已经尝试过 Int(gamesWon), gamesWon as!Int,但这不起作用。如何将其转换为整数?
class Parent
{};
class A_child : public Parent
{
void A_method();
};
class B_child : public Parent
{
void B_method();
};
void main()
{
A_child a;
Parent *p = &a;
B_child *b = (B_child*)&p;
b->B_method();
}
Run Code Online (Sandbox Code Playgroud)
这段代码是用C++编写的.这是一个逻辑错误,因为我们试图将"猫"变成"狗".但它的确有效.谁能解释为什么以及如何解释?
我有一堂这样的课:
struct Base
{
void aa(int n) const {
std::cout << "aa() " << field*n << std::endl;
}
void bb(int n) const {
std::cout << "bb() " << field*n*2 << std::endl;
}
int field = 2;
};
Run Code Online (Sandbox Code Playgroud)
我希望能够在编译时选择两个实现之一,aa()
或者bb()
,通过调用操作符方法。就像是:
Base data;
Magic obj(data);
obj.as_AA() * 33; // should call data.aa(33)
obj.as_BB() * 44; // should call data.bb(44)
Run Code Online (Sandbox Code Playgroud)
data
不得重复。并且aa()
vs的选择bb()
必须在编译时解决。
我有一个使用向下转换的解决方案,其行为理论上未定义(我认为)。它构建(使用 g++ 和 clang++)并完美运行,但仍然......
struct AA : public Base
{
void operator*(int …
Run Code Online (Sandbox Code Playgroud) 我想知道在没有未定义行为的static_cast
情况下应用向下转型的简短代码示例。我环顾四周,发现很多文本(帖子、问答等)引用了标准的片段、解释等。但我没有找到任何例子来说明这一点(这让我感到惊讶)。
谁能提供一个这样的例子吗?
这是我的例子:
public class Person
{
public string Name { get; set; }
}
public class Client : Person
{
public string LastName { get; set; }
}
public class Test
{
Person p = new Person();
Client c = (Client)p; //throws exception
}
Run Code Online (Sandbox Code Playgroud)
由于客户端继承自Person,为什么我不能这样做呢?如果可以,这是错误的方式,我该怎么办?
OBS:我知道上面的例子会:
Person p = new Client();
Client c = (Client)p;
Run Code Online (Sandbox Code Playgroud) 所以我无法想象我的生活.我尝试过多种形式的演员.
这是父母.
/// <summary>
/// Colour Parent
/// </summary>
public class Colour
{
#region Public Fields
public float R;
public float G;
public float B;
public float A;
#endregion Public Fields
}
Run Code Online (Sandbox Code Playgroud)
而这就是孩子.哪个有预先定义的变量.意味着它具有向下传播所需的所有信息.
/// <summary>
/// Data strucutre extension of Base.Colour, Used for the variables stored inside the glow object in memory
/// </summary>
public class GlowStruct : Colour
{
public bool RWO = true;
public bool RWUO = true;
}
Run Code Online (Sandbox Code Playgroud)
我试着用它来施展.
return Base.Blue as GlowStruct;
Run Code Online (Sandbox Code Playgroud)
Base.Blue是Color类的静态成员.使用"is"返回false.
不知道为什么你需要Base.Blue的定义看作是它的正数.我已经提到它是一个静态类.
public static …
Run Code Online (Sandbox Code Playgroud)