Hello. I have an obscure question for y'all. Consider an inner class: -
class Outer1 {
-
class Inner1 {
-
}
-
}
-
Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below? -
class Outer2 {
-
Inner1 i;
-
-
class Inner2 extends Outer1.Inner1{
-
Inner2(Outer1 o) {
-
o.super();
-
}
-
}
-
-
public void aMethod(Outer1 o) {
-
if (<i is an inner class of o>) {
-
....
-
}
-
}
-
}
-
6 2074
Hello. I have an obscure question for y'all. Consider an inner class: -
class Outer1 {
-
class Inner1 {
-
}
-
}
-
Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below? -
class Outer2 {
-
Inner1 i;
-
-
class Inner2 extends Outer1.Inner1{
-
Inner2(Outer1 o) {
-
o.super();
-
}
-
}
-
-
public void aMethod(Outer1 o) {
-
if (<i is an inner class of o>) {
-
....
-
}
-
}
-
}
-
Your code is not correct. Inner1 i; will give a compilation error. You can only declare objects of type Inner1 by getting in the context of Inner1 first which is Outer1. As in Outer1.Inner1 i;
Now your question does not make much sense since inner classes are members of classes not of objects. A class is an inner class of another class not of an object. If you change your question to how to test if a class is an inner of a given class, then such a question would be redundant since the syntax would require something like
if Outer.Inner isInnerOf Outer and you can see that it is both redundant and impractical to have such a comparison.
Here is a full working example which I just compiled: -
public class Outter2 {
-
public static void main(String[] args) {
-
Outter1 o1 = new Outter1();
-
Outter2 o2 = new Outter2(o1);
-
o2.aMethod(new Outter1());
-
}
-
-
Inner2 i2;
-
-
Outter2(Outter1 innerParent) {
-
i2 = new Inner2(innerParent);
-
}
-
-
void aMethod(Outter1 o) {
-
System.out.println("is o the parent object of i2?");
-
}
-
-
class Inner2 extends Outter1.Inner1 {
-
Inner2(Outter1 parent) {
-
parent.super();
-
System.out.println("success!");
-
}
-
}
-
}
-
-
class Outter1 {
-
class Inner1 {
-
Inner1() {
-
System.out.println("success?");
-
}
-
}
-
}
-
The output is:
success?
success!
is o the parent object of i2?
Here is a full working example which I just compiled: -
public class Outter2 {
-
public static void main(String[] args) {
-
Outter1 o1 = new Outter1();
-
Outter2 o2 = new Outter2(o1);
-
o2.aMethod(new Outter1());
-
}
-
-
Inner2 i2;
-
-
Outter2(Outter1 innerParent) {
-
i2 = new Inner2(innerParent);
-
}
-
-
void aMethod(Outter1 o) {
-
System.out.println("is o the parent object of i2?");
-
}
-
-
class Inner2 extends Outter1.Inner1 {
-
Inner2(Outter1 parent) {
-
parent.super();
-
System.out.println("success!");
-
}
-
}
-
}
-
-
class Outter1 {
-
class Inner1 {
-
Inner1() {
-
System.out.println("success?");
-
}
-
}
-
}
-
The output is:
success?
success!
is o the parent object of i2?
What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.
What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.
Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?
Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?
Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package
Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package
Is anyone sure whether I have to use reflection to accomplish this test? I am in a situation to need that information in a project I'm working on.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Murat Tasan |
last post by:
hi, i am having a small problem with constructing an inner class.
i am using an inner class (and not a static nested class) because the
methods of the inner class need access to the enclosing object's members.
now, i have an enclosing class, which has a static factory method (loads
an instance of the enclosing class from a serialized file, and returns
it).
during the loading, some initialization takes place. one of the
initialization...
|
by: devu |
last post by:
Currently I'm working on a class that performs a batch process as per
scheduling for all employees in a company. Being a batch process load
is obviously a huge factor.
The process first needs to calculate the eligibility of each employee
before proceeding to the next stage. I need to cache some of the
employee information if an employee is found eligible for later use in
the next stage.
One option could be a javabean style value object...
|
by: Paul Morrow |
last post by:
I'm hoping that someone can explain why I get the following exception.
When I execute the code...
######################################
class Parent(object):
class Foo(object):
baz = 'hello from Parent.Foo'
class Child(Parent):
#Foo.baz = 'hello from Child.Foo'
|
by: MariusI |
last post by:
Consider the following class layout
public class Order
{
public ProductOrder AddProductOrder(/* variables required to create a
product order */)
{
/* Check if the product order can be added to the order */
}
|
by: Mark E. Fenner |
last post by:
Hello all,
I have a code where my inner loop looks like:
allNew =
for params in cases:
newObj = copy(initialObject)
newObj.modify(params)
allNew.append(newObj)
return allNew
| |
by: Martijn Mulder |
last post by:
A construction like this:
class Outer
{
class Inner:Outer
{
}
}
compiles without problem but does it introduce infinity?
|
by: mrstephengross |
last post by:
I'm making progress on mixing templates with friends (sounds like a
drinking game, huh?). Anyway, here's the situation. I've got an "Outer"
class with a private "Inner" class (sub-class, technically). I want to
declaring an unrelated "Thing" class to be a friend of the inner class.
However, since the inner class is private, the compiler complains.
Here's what the code looks like:
====================================================...
|
by: andrew_nuss |
last post by:
Hi,
I have an Array<Ttemplate that specializes on T's in my program that
inherit from a base class that I've defined called CtorDtorLess. In
my array template, I check if boost::is_convertible<T*, CtorDtorLess*>
returns true, and then don't use placement new to construct.
I've checked in the debugger and everything works with T's that are
public classes. However, I also have some "inner classes" that are
declared in the private...
|
by: Matthias Buelow |
last post by:
Hi folks,
I've got something like:
class Outer {
int f();
friend class Inner;
class Inner {
int g() {
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |