how can i access get father name and store it to - swift

class parent{
var fathername:String="sugumar"
}
class child:parent{
var car:String }
how can I get father name from the base class and store it to the property child in derived class child

Let's assume you have created an instance of the child class. Accessing the fathername variable of the parent class is just as same as accessing the car variable of the child class.
let childInstance = child()
print(childInstance.fathername)
print(childInstance.car)
You might wanna give a value to the car variable before creating the instance.

Related

Do I have to create both getter and setter for a field?

Say I have a class like this :
class Product {
int id;
Product([this.id]);
}
And I allow my product id to be changed. So, am I good with above or should I use below model
class Product {
String _id;
Product([this._id]);
set id(id) => _id = id; // setter
get id => _id; // getter
}
It depends on the use of you class, for instance if the class is a model class for instance
(Employee class)which is only used to save some data and no functionality,
then it should include a setter and getter for its member variables.
But if there is service layer class, which uses Employee class and performs certain operations on it, then use of setter and getter is not a must.
Getters and setters are created automatically for non-private class instance variables. So your top code is fine for reading and updating the id variable from anywhere.

How to call the declare type methods avoiding dynamic binding?

Person[] p = new Person[5];
p[0] = new Student();
p[1] = new Student();
...
Arrays.sort(p);
Here Person is the parent class and Student is the child class. There is an overridden compareTo() method both in Person class and Student class. I want to sort Person array based on the method in Person class not in Student class. But JAVA's dynamic binding calling the method in Student class as Student is the actual type. How to solve it?

about constant defined in a singleton class

If I define a singleton in Swift 4:
public class MySingleton {
static let shared = MySingleton()
}
Callers can access the singleton instance by MySingleton.shared.
If I add a constant field school in MySingleton :
public class MySingleton {
static let shared = MySingleton()
let school = School()
}
Callers could access school by MySingleton.shared.school. Am I right that if all callers access school by this way, there would be only one instance of School in my iOS application?
Am I right that if all callers access school by this way, there would be only one instance of School in my iOS application?
In theory, maybe. In fact, maybe not. The problem is that school is an instance property. You say that callers could say MySingleton.shared.school — but the problem is, they might not. They could also say MySingleton().school. And if they do that, each one who says it will have a different School instance.
Why not to create a singleton for your school?
class School {
static let shared = School()
private init() { }
}
instance of the school will be different every time I call MySingleton().school
If all callers access school that way and no other instances are created, then it will be the only instance.
If School is a struct, then any attempt to store the school object in another variable will effectively create a new instance.
So for example:
var s = MySingleton.instance.school // where School is a struct.
In the above, s is conceptually a different instance than MySingleton.instance.school and changing one will not be reflected in the other.

How do I do multiple level eager loading in Entity Framework 4.3 when I have a polymorphic collection?

I have the following classes:
public class Parent
{
public ICollection<Child> Children {get;set;}
}
public class Child
{
}
public class Boy : Child
{
public Toy Toy {get;set;}
}
public class Girl : Child
{
public Book Book {get;set;}
}
I want to load a parent with all boys eagerly:
Parents.Include(p => p.Children.OfType<Boys>().Select(b => b.Toy));
The above does not work and I get the error saying that the path is invalid.
How do I accomplish this?
I think in this case the extension method Include resolves to a string "Boys" which, obviously, can not be included by the member method Include.
Even if this would run, I suspect it would be a problem to have a Children collections that is only filled with Boy objects. For me that would be an indeterminate state of the collection, because it represents the children of the parent. So it either should contain all children or yet be empty.
If you often need the Boys collection (or the real life pendant of it) and its references (Toy) you should map it as a separate navigation property. Otherwise do the OfType() on the Children collection.

row specific class

How do I create a Zend_Db_Table which returns a different class for each row.?
Example
UserTable has id,name and type
Type contains class names (admin,client,etc...)
The classes admin, client are all subclasses of user
If I call fetch I need to get a admin or client object depending on the corresponding value in the db.
class Your_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
}
class Your_Db_Table extends Zend_Db_Table_Abstract
{
protected $_rowClass = "Your_Db_Table_Row";
}
or
new Your_Db_Table(array("rowClass" => "Your_Db_Table_Row");
So whenever you get a rowset from your table subclass, the rows included in it will be your custom class.
Edit
To get a custom row based on a value, I would say extend the Zend_Db_Table_Rowset_Abstract class instead and override this method:
getRow(int $position, [bool $seek = false])
You'll also need to override the current method and perhaps some of the other SeekableIterator implemetations which actually creates a row class based on the _rowClass property. You might be able to set the _rowClass before current is called based on your data's row type.
You could instantiate a specific class in current and return it based on the type parameter.
Have you though about maybe just using composition instead? Say just passing in data to a new class if it's an admin type or something?