Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoelAtMoodkie · Apr 22, 2010 at 11:02 PM · inheritancemethodsuper

Why does this run it's "super" method?

Here's a simplified example to explain my problem in Javascript:

I have a super-class:

class mySuperClass()
{
static function run()
{
return "mySuperClass";
}
}

And a sub-class which inherits from my super-class:

class mySubClass extends mySuperClass
{
static function run()
{
return "mySubClass";
}
}

And finally, a 3rd class:

class doThings
{
function Start()
{
var aClass = new mySubClass();
}
function Update()
{
Debug.Log(aClass); //This displays the text 'mySubClass'
var myText = aClass.run();
}
}

1) How comes myText equals "mySuperClass"? Surely it should run the mySubClass.run() method instead of the mySuperClass.run() method?

2) Is there any way to make it so that the super-class can never be instantiated (I know interface and abstract isn't supported)?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Molix · Apr 22, 2010 at 11:46 PM 0
Share

It worked ok for me, but I had to adjust it slightly to get it to compile. I realize you have simplified it for the example, but it may have lost something in translation.

class mySuperClass { static function run() { return "mySuperClass run"; } }

class mySubClass extends mySuperClass { static function run() { return "mySubClass run"; } }

var aClass;

function Start() { aClass = new mySubClass(); } function Update() { Debug.Log(aClass); //This displays the text 'mySubClass' var myText = aClass.run(); Debug.Log("myText = " + myText); }

avatar image JoelAtMoodkie · Apr 23, 2010 at 12:22 AM 1
Share

Thanks for your time $$anonymous$$olix. I've managed to solve the problem ... I don't think the example I gave actually had the problem I was having in my actual code. $$anonymous$$aking my superclass functions Virtual solved the problem. Again, thanks for the effort!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JoelAtMoodkie · Apr 23, 2010 at 12:19 AM

I appear to have solved this myself.

I needed to make the functions in my superclass 'virtual' functions ... for some reason I thought virtual wasn't supported in Javascript, and thus I am a fool ;)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image qJake · Apr 23, 2010 at 02:43 AM 2
Share

The Unity implementation of JavaScript isn't actually JavaScript. It's more... just the syntax of JavaScript adopted to .NET/$$anonymous$$ono. It's a pretty poor implementation, too, if you ask me. C# is a whole lot easier to understand and code with in the long run.

avatar image Ray-Pendergraph · Apr 23, 2010 at 03:11 PM 0
Share

If we could vote up comments, I would vote up SpikeX's comment. $$anonymous$$ono JS is just enough like your browser's JS to be annoying (and too different to be useful as a "javascript implementation " I$$anonymous$$HO). If you are doing anything serious you should seriously consider using C#. It's more structured, but like Spike said, that buys you a lot in the long run.

avatar image ericksson · Apr 24, 2010 at 08:52 AM 0
Share

You can vote up comments! Just hover your mouse to the left side of the comment and you have an up arrow you can press to vote for the comment.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Inventroy Problem- Using different parameters in inherited classes 1 Answer

Interaction script 2 Answers

Null Reference Exception: Object Reference, with base class? 1 Answer

Confused about the pre-defined word "super" 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges