C # enumeration, inherited applications-simulated animals, frogs, and dogs all inherit animals

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace ooptext
{
/// <Summary>
/// Simulate that both the dog and the frog are animals and use the facial image object idea to design them.
/// All Abstract: they are animals.
/// All have animal characteristics
/// </Summary>
Public class animal
{
Private int mood;
/// <Summary>
/// This attribute of an animal is used to determine the mood. It can be set by its Sub-object.
/// </Summary>
Public int mood
{
Get {return mood ;}
Set {mood = value ;}
}
Protected bool mammal = true; // whether it is a mammal
Static void main ()
{
Dog dog = new dog ();
Dog. Mood = 1;
Console. writeline (dog. sayhi ());
Frog frog = new frog ();
Frog. Mood = 2;
Console. writeline (frog. sayhi ());
Console. writeline (frog. islaysegg (true ));
Console. Readline ();
}
}
/// <Summary>
/// Use enumeration to describe the mood of an animal
/// </Summary>
Public Enum seek
{
Scared = 1, // good mood
Comforted = 2 // bad mood
}
Public class Dog: Animal
{
Public dog ()
{
// This member inherits the animal
This. mammal = true; // It is a mammal
}
Public String sayhi ()
{
Switch (mood)
{
// Note: this is an enumeration type. You also need to convert it to the int type to obtain the value.
// This is the details: Do not
Case (INT) Seek. Scared:
Return ("the dog is in a bad mood. Why !! ");
Case (INT) Seek. comforted:
Return ("the dog is in a good mood, Wangwang !! ");
}
Return ("the dog is in a bad mood. shake its tail !!! ");
}
}
Public class frog: Animal
{
Public Frog ()
{
This. mammal = false; // It is not a mammal
}
Public String sayhi ()
{
Switch (mood)
{
Case (INT) Seek. Scared:
Return ("the frog has a bad taste. I jumped into the river !!! ");
Case (INT) Seek. comforted:
Return ("The frog is in a good mood !!! ");
}
Return ("The frog is in a bad mood !!! ");
}
Public String islaysegg (bool Islay)
{
If (Islay)
{
Return ("the frog has to lay eggs ");
}
Else
{
Return ("the frog does not lay eggs !!! ");
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.