LESS: Inheritance using a variable
Assuming you use WE2012 that includes Less 1.4.2 the simplest solution would be:
@import (less) "../font-awesome.css";
.icon-application-home {
&:extend(.fa, .fa-home all);
}
Or:
@import (less) "../font-awesome.css";
.icon-application-home
:extend(.fa, .fa-home all) {
}
Read extend
documentation for details on how this stuff works.
If you upgrade to an IDE/Compiler incorporating Less 1.6.x you will be able to do:
@import ".../font-awesome.less"
.icon-application-home {
.fa;
&:before {content: @fa-var-home}
}
There you still can't use .fa-home
or .fa-home:before
as mixins since the first is not defined and the second is not valid mixin selector, fortunately &:before {content: @fa-var-home}
is just what .fa-home
does.
In general though, the extend
based solution is more optimal since it produces more compact CSS.
Inheritance with variables overriding
Ok this question seems to be a duplicate of "Can I override global variables from the calling scope". And I found an answer in its comments (https://gist.github.com/seven-phases-max/9473260) but this changes the original class a little bit:
@var: 'value';
.lib() {
.class() {
property: @var;
// and the rest of `.class`
}
}
.class {
.lib.class();
}
.another-class {
@var: 'another-value';
.lib.class();
}
Can a derived class be smaller than its parent class?
Can a derived class be smaller than its parent class?
No. A derived class always contains a base class sub object. An object can never be smaller than its sub objects, so a derived class can never be smaller than its base.
Is the only way to make this happen by replacing the 'name' field in Person with a get_name() function that we simply override in PersonNamedJared to always return "Jared"?
That would be one way to achieve it.
How would we still make the name variable in the base class?
You couldn't have a member variable in a base that you don't want to be in the derived class.
You could for example use multiple inheritance so that you do have a base with the variable in some derived classes. Something like this:
struct Person {
unsigned int age;
virtual std::string name() = 0;
...
struct Named {
std::string name;
};
struct NamedPerson : Person, private Named {
std::string name() override {
return name;
}
};
struct JaredPerson : Person {
std::string name() override {
return "Jared";
}
};
Here, there is a base with the variable, but Jared does not inherit that particular base.
Is there a way to override class variables in Java?
Yes. But as the variable is concerned it is overwrite (Giving new value to variable. Giving new definition to the function is Override). Just don't declare the variable but initialize (change) in the constructor or static block.
The value will get reflected when using in the blocks of parent class
if the variable is static then change the value during initialization itself with static block,
class Son extends Dad {
static {
me = "son";
}
}
or else change in constructor.
You can also change the value later in any blocks. It will get reflected in super class
Python inherit variables from parent class
You could try passing a Parent
instance to the Child
initializer...That's probably the closest you'll get.
class Parent:
def __init__(self, eye_color, length):
self.eye_color = str(eye_color)
self.length = length
class Child(Parent):
def __init__(self, gender, parent):
super().__init__(parent.eye_color, parent.length)
self.gender = str(gender)
x = Parent("Blue", 2)
y = Child("Men", x)
print(x.length, x.eye_color)
print(y.gender, x.length)
Another thing you could do is hold a last_parent
variable:
global last_parent
class Parent:
def __init__(self, eye_color, length):
self.eye_color = str(eye_color)
self.length = length
last_parent = self
class Child(Parent):
def __init__(self, gender):
super().__init__(last_parent.eye_color, last_parent.length)
self.gender = str(gender)
x = Parent("Blue", 2)
y = Child("Men")
print(x.length, x.eye_color)
print(y.gender, x.length)
How to create a child class with one less constructor parameter from the parent constructor in java
public class LongShelfLife extends Perishable {
public LongShelfLife(String name, double price) {
super(name, 720, price);
}
}
Variable Inheritance in Java
Fields are not virtual, unlike methods. For this reason, it is a bad idea to declare fields with the same name as a field in another class in the hierarchy. The field referred to in theMethod
is always going to be from Big
(i.e. when you declare a field with the same name, it just hides the old field when in the scope of the replacing class, but doesn't replace it).
One solution would be to override a method that gets the field from the current class:
In theMethod
replace the tellMe
field with getTellMe()
and for all classes override getTellMe()
to return the correct value (or the field that hides the superclass's field).
Inheritance - change variable upon initialization based on child value
What you need to do is declare the property in base class and provide an option for the child class to override it. This will allow you to also provide a default value if you want to.
Some thing like this:
public class Tile
{
private string _texturePath = String.Empty;
private Texture2D _texture;
protected virtual string TexturePath { private get { return _texturePath; } set { _texturePath = value; } }
public Tile()
{
if (!string.IsNullOrWhiteSpace(TexturePath))
_texture = LoadTexture(TexturePath);
}
private Texture2D LoadTexture(string texturePath)
{
throw new NotImplementedException();
}
}
internal class Texture2D
{
}
public sealed class Free:Tile
{
protected override string TexturePath
{
set
{
if (value == null) throw new ArgumentNullException("value");
base.TexturePath = "Content/wall.png";
}
}
}
In case you do not want to provide a default texture path, you can plan to make the property and the base class abstract.
How to access class variables? public methods/getters VS. inheritance. What are pros and cons
You should not inherit publicly unless the class that you are writing indeed extends the class from which you are inheriting, i.e. manageSomething
is a kind of DataStructure
. This is known as Liskov substitution principle. Violating it leads to serious readability and maintainability issues in your code.
The rules are less strict with private inheritance, but in general you should prefer composition to private inheritance, except under very specific circumstances.
The upshot of this is that your first code snippet (with the composition) does everything right, you do not need to change it.
C# Member variable inheritance
Child class will inherit members of parent class. You don't need to specify them. Also it's better to use properties rather than public fields.
public class MapTile
{
public Texture2D Texture { get; set; }
public Rectangle MapRectangle { get; set; }
public MapTile(Rectangle rectangle)
{
MapRectangle = rectangle;
}
}
public class GrassTile : MapTile
{
public GrassTile(Rectangle rectangle) : base(rectangle)
{
Texture = Main.GrassTileTexture;
}
}
Related Topics
How to Overwrite CSS Variable with Its Own Value
Z-Index Between Children and Parents
Passing CSS Property:Value as SASS Mixin Argument
CSS Problem with Background-Attachment:Fixed;
Reading Documents CSS in Chrome Extension
(Sass) How to Exclude My Imported Files to Compile to CSS Files
How to Set the 'Left' Property of My Div Using CSS3 Calc
How to Set CSS Hover Effect, on Parent and Child Elements
Disable Bootstrap for One Element
CSS Selector When :Target Empty
What's the Difference Between CSS3 Translate Method and CSS2 Relative Positioning
How to Use Skew Only in the Parent Element
Concatenate String in Less in Loop