C # several types of reference type conversion,
Conversion of reference types in this article: convert child classes to parent classes, convert parent classes to child classes, and not between child parent-level relational classes.
□Implicit conversion: Convert subclass to parent class
public class Animal
{
public int _age;
public Animal(int age)
{
this._age = age;
}
}
public class Dog : Animal
{
public float _weight;
public Dog(float weight, int age) : base(age)
{
_weight = weight;
}
}
Client, subclass into parent class.
static void Main(string[] args)
{
Dog dog = new Dog(2.5f,12);
Animal animal = dog;
Console.WriteLine(animal._age);
}
Result: 12
It can be seen that implicit conversion is used to convert subclass to parent class. This type of conversion is completed on the stack. The stack first has the variable dog that represents the subclass, then the variable animal that represents the parent class, and finally assigned the heap address saved by dog to anmial.
□Forced conversion: the parent class is converted to a subclass.
If the parent class of the client is converted to a subclass.
static void Main(string[] args)
{
Animal animal = new Animal(12);
Dog dog = (Dog)animal;
Dog dog = animal as Dog;
if (dog != null)
{
Console.WriteLine(dog._age);
}
else
{
Console. WriteLine ("Conversion failed ");
}
}
Result: An exception is thrown. Animal cannot be converted to Dog.
It can be seen that the parent class is converted into a subclass using the above method, and an exception will be thrown if the conversion fails.
□Use as to convert: parent class to subclass
If the client uses as to convert the parent class to a subclass.
static void Main(string[] args)
{
Animal animal = new Animal(12);
Dog dog = animal as Dog;
if (dog != null)
{
Console.WriteLine(dog._age);
}
else
{
Console. WriteLine ("Conversion failed ");
}
}
Result: The conversion fails.
It can be seen that the parent class is converted into a subclass using as, and no exception is thrown if the conversion fails.
□Use is to judge before strong conversion: Convert the parent class to a subclass
Before Strong conversion, you can use is to determine whether the parent class can be converted into a subclass, and then determine whether the conversion is successful based on whether the subclass instance is null.
static void Main(string[] args)
{
Animal animal = new Animal(12);
Dog dog = null;
if (animal is Dog)
{
dog = (Dog)animal;
}
if (dog == null)
{
Console. WriteLine ("Conversion failed ");
}
else
{
Console. WriteLine ("converted ");
}
}
□Use operators to achieve strong Conversion
You can design a static, implicit, and operator method in a class to convert this class instance to another target conversion object instance.
public class Benz
{
public int Mile { get; set; }
public Benz(int mile)
{
Mile = mile;
}
public static implicit operator Car(Benz benz)
{
return new Car(){Mile = benz.Mile};
}
}
public class Car
{
public int Mile { get; set; }
}
Client
static void Main(string[] args)
{
Benz benz = new Benz(1000);
Car car = benz;
Console.WriteLine(car.Mile);
Console.ReadKey();
}
Result: 1000
○ Use operators to establish relationships between the two classes that are unrelated to each other to achieve Conversion
○ When Car car = benz is executed, operator Car of the Benz class will be executed.
○ The operator method must meet the following conditions: static, implicit, and the name must be consistent with the class name to be converted. The class instance to be converted is returned.
○ When Car car = benz is executed, a Car instance is created on the stack and assigned to the variable car on the stack.
Summary:
○ Implicit conversion of sub-classes into parent classes. In essence, a variable value on the stack is assigned to another variable on the stack.
○ Convert a parent class to a subclass. If you directly use the "(subclass) parent class instance" method, an exception is easily thrown.
○ Convert the parent class to a subclass. If you use as, you can avoid throwing an exception.
○ Conversion of the parent class to the like. You can also use the is command before conversion.
○ Two classes that are not child-parent relationships, you can design an operator method in one class to convert the class instance to the target object instance.
In C language-> what?
-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */
For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!
In C language-> what?
-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */
For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!