how to set attributes of class in python code example
Example 1: c# get all class properties
//through reflection
using System.Reflection;
//Get a List of the properties from a type
public static PropertyInfo[] ListOfPropertiesFromInstance(Type AType)
{
if (InstanceOfAType == null) return null;
return AType.GetProperties(BindingFlags.Public);
}
//Get a List of the properties from a instance of a class
public static PropertyInfo[] ListOfPropertiesFromInstance(object InstanceOfAType)
{
if (InstanceOfAType == null) return null;
Type TheType = InstanceOfAType.GetType();
return TheType.GetProperties(BindingFlags.Public);
}
//purrfect for usage example and Get a Map of the properties from a instance of a class
public static Dictionary<string, object> DictionaryOfPropertiesFromInstance(object InstanceOfAType)
{
if (InstanceOfAType == null) return null;
Type TheType = InstanceOfAType.GetType();
PropertyInfo[] Properties = TheType.GetProperties(BindingFlags.Public);
Dictionary<string, PropertyInfo> PropertiesMap = new Dictionary<string, PropertyInfo>();
foreach (PropertyInfo Prop in Properties)
{
PropertiesMap.Add(Prop.Name, Prop);
}
return PropertiesMap;
}
Example 2: python define class
class uneclasse():
def __init__(self):
pass
def something(self):
pass
xx = uneclasse()
xx.something()
Example 3: how to declare a class in python
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
def __init__(self, var1=0, var2):
#the name of the construct must be "__init__" or it won't work
#the arguments "self" is mandatory but you can add more if you want
self.age = var1
self.name = var2
#the construct will be execute when you declare an instance of this class
def otherFunction(self):
#the other one work like any basic fonction but in every methods,
#the first argument (here "self") return to the class in which you are
Example 4: using class in java
public class HelloWorld {
public static void main(String[] args) {
// how to use class in java
class User{
int score;
}
User dave = new User();
dave.score = 20;
System.out.println(dave.score);
}
}