As I know, if the static method from other class, we may interpret their relationship with dependency, just like the answer from How to show usage of static methods UML Class Diagram
However, how about for the static variable from other class? Is it the similar case; using dependancy?
For example,
class A{
public static String CHAR="Charecter";
public static String INT="Integer";
public static String STR="String";
}
class B{
public String Type;
public B(){
Type=STR;
}
public void B(String t){
Type=t;
}
}
would it result in the uml class diagram as below?
Note that although I would rather use enumeration in real life for this case, I just would like to know how it works.
Yes, this is similar.
Dependency shows that one class is "aware" of some other class and uses it in some way (or more generally depends on it). It can for instance refer to (public) static attributes, static operations and so on.
Related
when I create an interface object implementing the abstract methods (through anonymous classes). What type of object am I creating?
with an example the question will be better understood
public class Class1{
public static void main(String...args){
Interface1 object1= new Interface1(){
#Override
public method1(){
System.out.println("im the method");
}
}
System.out.println(Object1.getClass());
}
this returns the class Class1. how can you explain this?
an explanation of my doubt
I would like to know if I can access the constructor of the base class in its derived classes in C#. If yes please let me know how could we make it. Thanks in advance.
You can call the base class constructor as part of the execution of the derived class constructor
public MyBase
{
public MyBase() { }
}
public Derived
{
public Derived() : base() { }
}
When using this pattern, you are said to be using the base class initializer.
For more background, see the base keyword and instance constructors on MSDN.
In my projects, I often have many static Objects of a class (let's say class A).
I create an additional class B to store the static Objects of class A, because else it would be too messy to store all static Objects in their own class:
public class A{
public A(...){
...
}
}
public class B{
public static A a1 = new A(...);
public static A a2 = new A(...);
public static A a3 = new A(...);
...
}
Then I access these Objects with B.a1, B.a2, ...
Is there a better way to store a large number of Objects?
This is a bit too general to answer. Why exactly do you need these static instances?
If it's a small number of commonly used instances, it really makes the most sense for them to be within the class itself. Look at BigInteger, which has static instances for 0, 1, and 10 within the class.
Consider the following code:
// ======== Abstract class ========
public abstract class Creatures {
public abstract void loseEnergy();
public void execute()
{
loseEnergy();
}
}
// ======== Animals ========
public class Animals : Creatures
{
public override void loseEnergy(){}
}
public class Birds : Animals
{
public override void loseEnergy(){}
}
// ======== Human ========
public class Human : Creatures
{
public override void loseEnergy(){}
}
public class Male : Human
{
public override void loseEnergy(){}
}
public class Female : Human
{
public override void loseEnergy(){}
}
[ This code was based on the code by Jayson suggested here: "Base class methods calling derived class methods ?" ]
In the given code example, I would like to have the runtime executing EACH derived class object's certain method, in this case, which is 'loseEnergy()', however, I could not find the solution.
How do I approach this problem?
What can be useful to know or to try.. in order to solve this issue?
Your help is very much appreciated!
Thank you!
Kind regards,
Segara
P.S. Some search I have done so far:
"How to call overriden methods in all derived classes"
"Collection of derived classes that have generic base class"
"How to call derived function using base class object"
"Call method of the derived class through reflection possible or no"
EDIT:
I decided to stick to the idea I had before which is to have some list that would contain the objects of the classes that have 'loseEnergy()' method. Having such list I will be able to call every object's method 'loseEnergy()', which is what I wanted.
Question can be closed.
Thank you.
I didn't really understand your problem but anyway i can try to give you some means to use abstract classes :
If you use a abstract method, you SHOULD override it in a subclasses (like a method declared in an interface)
If you want that all inherited class use a same method, you can implement it in the abstract class ; all subclasses will use the method you implements if you don't override it, you've have to not declare it in the subclasses (extends < ABS_CLASS > is good enough)
If you want use a method of the abstract class which is override in the sub class you can use the keyword super .
I hope it will help you.
if you mean that you want the calls: female.loseEnergy() -> human.loseEnergy() -> creature.loseEnergy(), call the base method in the first line of the overriden one
public class Female : Human
{
public override void loseEnergy()
{
base.loseEnergy();
// do stuff
}
}
In the Greenfoot environment that you mention in the post above, the act() method is called only on actors which have been added into the "world". Internally, this adds them into a list. The simulation process iterates through the list and calls act() on each object in turn. Objects that are not "in the world" are not known to the system and so do not have their act method called. There is no magic here going on here.
If you wanted similar behaviour but without manually adding objects into a list, you could possibly have the base class constructor add new objects into a global list. I don't know C# so I don't know precisely how to do this, but I cannot imagine it would be difficult.
What is the importance of using SecurityPermission in a Serializable class?
In an article on [Microsoft's Site][1], they suggest that you write a Serializable class as follows:
[Serializable]
public class PleaseSaveMe : ISerializable
{
public readonly int Age;
public readonly string Name;
public int KarateSkills;
public PleaseSaveMe(int Age, string Name)
{
this.Age = Age;
this.Name = Name;
}
// Serialization Methods
protected PleaseSaveMe(SerializationInfo info, StreamingContext context)
{
Age = info.GetInt32("Age");
Name = info.GetString("Name");
KarateSkills = info.GetInt32("KarateSkills");
}
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Age", Age);
info.AddValue("Name", Name);
info.AddValue("KarateSkills", KarateSkills);
}
}
But in the documentation of SecurityAction.LinkDemand, it says specifically to NOT use it in .NET 4.0 (which is what I am using). What should I use instead? Is that attribute even necessary?
William
Well, with the [Serializable] attribute, you explicitly permit code to mess with your private parts. Without the security attribute, any code running in your process could create an instance of your class that is deserialized from data that wasn't necessarily saved by your serialization code. Possibly creating an object of your class that is in an inconsistent state that might thus be exploitable.
With the attribute, you can explicitly deny code that you don't trust well enough to do this. Which of course is a Good Thing.
And yes, CAS was deprecated in .NET 4, largely because so many programmers had basic questions like yours. CAS is definitely hard to understand and security that's difficult to comprehend is usually unsecure. It was replaced by a sandboxing model, the MSDN article is here. A backgrounder magazine article is here. In general, you only worry about this when you permit code to load in your process that came from a source you can't control or trust. Think plugins or insufficiently secured storage locations for assemblies.