ZackZak
|
  |
| Joined: 26 Jul 2009 |
| Total Posts: 2811 |
|
|
| 12 Jul 2012 05:49 PM |
Does anyone have any idea why I wouldn't be able to access variable c from class b?
public class Instance { public Instance Parent; }
public class a extends Instance { public int c = 42; public Instance Child = new b(this); }
public class b extends Instance { b(Instance p) { Parent = p; System.out.println(Parent.c); } } |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2012 06:03 PM |
THIS IS NOT JAVA
THIS IS ROBLOX LUA
GO BACK TO JAVAFEST
CODE IS WRONG |
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 12 Jul 2012 06:10 PM |
@Ub3r Nowhere does it state that only Rbx.Lua is allowed here. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2012 06:11 PM |
| How are we suppose to help him if others don't know Java? |
|
|
| Report Abuse |
|
|
WhiteRain
|
  |
| Joined: 24 Apr 2010 |
| Total Posts: 2723 |
|
|
| 12 Jul 2012 06:12 PM |
| There are various people who know Java. This forum is for basically any scripting/programming language help. OP, I suggest going to Scripters as they might have a bit more knowledge of Java as the majority of people on SH only know Rbx.Lua. If I knew Java I would help but I don't |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2012 06:13 PM |
| Plus I do Java and never seen someone use that code. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2012 06:20 PM |
| Lol, you guys don't get it... He's trying to sound little smarty. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2012 06:20 PM |
| In class b I don't see how it's like b(Public) something like that. |
|
|
| Report Abuse |
|
|
ZackZak
|
  |
| Joined: 26 Jul 2009 |
| Total Posts: 2811 |
|
|
| 12 Jul 2012 07:51 PM |
@uber:
That's the class constructor, which accepts a parent argument. |
|
|
| Report Abuse |
|
|
mamaguy
|
  |
| Joined: 07 Oct 2010 |
| Total Posts: 7073 |
|
|
| 12 Jul 2012 07:57 PM |
I would help, like white except I only know the GUI functions in java.(Learning more about it every day x3) Also, I think int needs to be Int and I don't think and integer and be public? o.O
|
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 08:11 PM |
| Wheres the main method? also you can only access class variables from a class method |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 12 Jul 2012 08:18 PM |
public class Instance { public Instance Parent; }
public class a extends Instance { public int c = 42; public Instance Child = new b(this); }
public class b extends Instance { b(Instance p) { // Here's your issue. Not all Instances are class a's; so not all of them will have a c property. Parent = p; System.out.println(Parent.c); // This should fail to compile as c is not a valid member of Instance. } } |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 12 Jul 2012 08:20 PM |
| Note: Whether or not the Instance passed to b's constructor is actually a class a (which would make the statement Parent.c valid) makes no difference to the compiler. Declare the constructor as taking b(a p) or use a cast. |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 08:20 PM |
Let's remark your errors:
public class Instance { //Theres no need to change the access, the dafault is more than enough in this case public Instance Parent; }
public class a extends Instance { public int c = 42; public Instance Child = new b(this); // You are trying to create a instance of a class that doesn't exist (at least in the part you posted) }
public class b extends Instance { b(Instance p) { //uh? is that a method? wheres the return type? Parent = p; System.out.println(Parent.c); } } |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 12 Jul 2012 08:22 PM |
@lombardo2
None of those are errors. 1) While unnecessary, is not wrong. 2) These are all in separate files (by Java rules) - b is defined. 3) That's not a method, it's a constructor. |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 08:30 PM |
I see, but isn't it simpler like this?:
class Parent{ String a = "This "; }
class Child extends Parent{ String b = "is "; }
class Childof extends Child{ String c = "inheritance"; }
class Main{ public static void main(String[] args){ Childof M = new Childof(); System.out.println(M.a + M.b + M.c); } } |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 12 Jul 2012 08:31 PM |
| Yes, but they aren't the same. |
|
|
| Report Abuse |
|
|
rayoma
|
  |
| Joined: 13 Nov 2009 |
| Total Posts: 1911 |
|
|
| 12 Jul 2012 08:33 PM |
@lombardo You're getting a bit confused... Swmaniac already pointed out the error. |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 08:36 PM |
| No, class b is defined below class a, there can't be another class b dude |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 08:36 PM |
| @rayoma Yes I didn't see well, I tough class b was extending class a, not class Instance |
|
|
| Report Abuse |
|
|
ballen7
|
  |
| Joined: 26 Sep 2008 |
| Total Posts: 819 |
|
|
| 12 Jul 2012 09:18 PM |
| Workspace.brick.CanCollide = false |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 12 Jul 2012 09:33 PM |
^Right...
Reading my posts again... what the hell I just said? I'm embarrassed |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 12 Jul 2012 09:55 PM |
This doesn't work because b doesn't inherit from a, it inherits from Instance.
|
|
|
| Report Abuse |
|
|
mamaguy
|
  |
| Joined: 07 Oct 2010 |
| Total Posts: 7073 |
|
| |
|
ZackZak
|
  |
| Joined: 26 Jul 2009 |
| Total Posts: 2811 |
|
| |
|