Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sugar syntaxic to access parent class member #14924

Closed
p0W3RH311 opened this issue Feb 28, 2021 · 2 comments
Closed

sugar syntaxic to access parent class member #14924

p0W3RH311 opened this issue Feb 28, 2021 · 2 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics

Comments

@p0W3RH311
Copy link

p0W3RH311 commented Feb 28, 2021

Hi guys !!

In Inheritance, we deal with base classes and child classes. If we have to intermediately access baseclass members including variables and methods then we need to have some mechanism. This mechanism of accessing the base class members is provided in PS using a ugly syntax

class Baseclass 
{
    $foo = 'he'
}
class Subclass : Baseclass  {
    $foo = 'llo'
    
    [string] print_foo() {
       return [Baseclass]::new().foo + $this.foo
    } 
}

[subclass]::new().print_foo()

output: hello

The above code shows how to access the member variables of the base class from the derived class when the member variables are declared with the same name both in the base as well as derived class. i use the standard syntax _[Baseclass]::new().foo_

class Baseclass 
{
    $foo = 'he'
}
class Subclass : Baseclass  {
    $foo = 'llo'
    
    [string] print_foo() {
        return  $base.foo + $this.foo
    } 
}

[subclass]::new().print_foo()

output: hello

i refer to the $foo variable of the base class using “$base.foo” while the derived class variable $foo is accessed wilth $this qualifier

class Baseclass 
{
     [string] print_foo() {
        return 5
    }
}
class Subclass : Baseclass  {
    $my = 6
    
    [int] print_foo() {
        return  $this.my + $base.print_foo()
    } 
}

[subclass]::new().print_foo()

output: 11

another example with different method signature

class Baseclass 
{
     [string] print_foo($msg) {
        return  'shell'
    }
}
class Subclass : Baseclass  
{
    $foo = 'power'
    $msg = 'world'

    [string] print_foo() {
        return  $this.foo + $base.print_foo($this.msg)
    } 
}

[subclass]::new().print_foo()

output: powershell

@p0W3RH311 p0W3RH311 added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Feb 28, 2021
@daxian-dbw daxian-dbw added the WG-Language parser, language semantics label Mar 10, 2021
@daxian-dbw daxian-dbw removed the Needs-Triage The issue is new and needs to be triaged by a work group. label May 6, 2021
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

2 participants