logo
down
shadow

Groovy: Accessing properties in Parent class


Groovy: Accessing properties in Parent class

By : rico
Date : November 22 2020, 10:40 AM
Hope that helps This has to be something really easy, but am pretty new to Grails & Groovy. I am writing a Grails application in version 2.4.3. , You are missing the list() method here:
code :
UserInfo.list().each {...}


Share : facebook icon twitter icon
Accessing Properties or a Parent Class from Base

Accessing Properties or a Parent Class from Base


By : Ahmad Khan
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further This is in general solved by utilize abstract functions at the base level and then overriding them with specifics in the derived class. So would would set up a call tree in the base but functions that need to know the details of the derived class would be implemented in the derived class.
code :
public class BaseModel
{
    protected string TableName { get; set; }

    public BaseModel()
    {         
    }

    public abstract void Save();
}
Accessing child class properties through function in parent class

Accessing child class properties through function in parent class


By : namika
Date : March 29 2020, 07:55 AM
this one helps. First, your Program.cs doesn't actually "do" what you say you want. It sounds like you want a program so that you can do this:
code :
Asset myAsset = new House();
myAsset.Rooms = 5;
if (myAsset is House)
    myAsset.Rooms = 5;
// Option 1, a Property Bag (Note: this replaces the properties on the classes)
class Asset
{
    Dictionary<string, object> myPropertyBag = new Dictionary<string, object>();

    public T GetProperty<T>(string property)
    {
        // This throws if the property doesn't exist
        return (T)myPropertyBag[property];
    }

    public void SetProperty<T>(string property, T value)
    {
        // This adds the property if it doesn't exist
        myPropertyBag[property] = (object)value;
    }
}

// Option 2, use a switch and override this function in derived classes
class Asset
{
    public int SomePropertyOnAsset { get; set; }

    public virtual T GetProperty<T>(string property)
    {
        switch (property)
        {
            case "SomePropertyOnAsset": return this.SomePropertyOnAsset;

            default: throw new ArgumentException("property");
        }
    }

    public virtual void SetProperty<T>(string property, T value)
    {
        switch (property)
        {
            case "SomePropertyOnAsset": this.SomePropertyOnAsset = (int)value;

            default: throw new ArgumentException("property");
        }
    }
}

class House : Asset
{
    public int Rooms { get; set; }

    public virtual T GetProperty<T>(string property)
    {
        switch (property)
        {
            case "Rooms": return this.Rooms;

            default: return base.GetProperty<T>(property);
        }
    }

    public virtual void SetProperty<T>(string property, T value)
    {
        switch (property)
        {
            case "Rooms": this.Rooms = (int)value; 
                break;

            default: base.SetProperty<T>(property, value);
                break;
        }
    }
}
// Option 1
Asset asset = new House();
asset.SetProperty("Rooms", 5);
var rooms = asset.GetProperty<int>("Rooms");

// Option 2
Asset asset = new House();
asset.SetProperty("Rooms", 5);
asset.SetProperty("SomePropertyOnAsset", 10);
asset.SetProperty("SomethingElse", 15); // Throws ArgumentException
Accessing Parent Class Properties from Property Objects

Accessing Parent Class Properties from Property Objects


By : WY Chang
Date : March 29 2020, 07:55 AM
With these it helps Without seeing any aditional code the best way to approach is by simply passing the required information as an argument to your GetVegetables method.
Ideally you should pass all the required data as an argument to make the methods very self-contained without relying on some global state.
Undefined property when accessing properties from a parent class

Undefined property when accessing properties from a parent class


By : zippyCow
Date : March 29 2020, 07:55 AM
To fix this issue I can't figure out how to access a property from class A using class B when class B is a child of class A and should inherits its properties using "extends". , put this in the constructor of class B()
code :
parent::__contruct();
Accessing Child class properties from parent class in drools

Accessing Child class properties from parent class in drools


By : Swapnava Chaudhuri
Date : March 29 2020, 07:55 AM
Hope this helps Drools cannot recognize what cannot be recognized.
If your class Car has a static method cast(Vehicle v), it should be recognized. (If there is no such method, you'll get an error.)
code :
class Car extends Vehicle {
    public static Car cast( Vehicle v ){ return (Car)v; }
    ...
}
(Car.class.cast(vehicle)).isManual()
function boolean checkIfCarManual(Vehicle vehicle){
    return vehicle.isCar() && ((Car)vehicle).isManual();
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org