C# 0 The Complete Reference


static  class called  NumericFn



Download 4,07 Mb.
Pdf ko'rish
bet289/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   285   286   287   288   289   290   291   292   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

static

 class called 



NumericFn

 to hold a set of 



static

 methods 

that operate on a numeric value. Because all of the members of 

NumericFn

 are declared 



static

,

the class can also be declared 



static

, which prevents it from being instantiated. Thus, 



NumericFn

serves an organization role, providing a good way to logically group related methods.

// Demonstrate a static class.

using System;

static class NumericFn {

  // Return the reciprocal of a value.

  static public double Reciprocal(double num) {

    return 1/num;

  }

  // Return the fractional part of a value.



  static public double FracPart(double num) {

    return num - (int) num;

  }

  // Return true if num is even.



  static public bool IsEven(double num) {

    return (num % 2) == 0 ? true : false;

  }

  // Return true if num is odd.



  static public bool IsOdd(double num) {

    return !IsEven(num);

  }

}

class StaticClassDemo {



  static void Main() {

    Console.WriteLine("Reciprocal of 5 is " +

                      NumericFn.Reciprocal(5.0));

www.freepdf-books.com




212

 

P a r t   I :  



T h e   C #   L a n g u a g e

    Console.WriteLine("Fractional part of 4.234 is " +

                      NumericFn.FracPart(4.234));

    if(NumericFn.IsEven(10))

      Console.WriteLine("10 is even.");

    if(NumericFn.IsOdd(5))

      Console.WriteLine("5 is odd.");

    // The following attempt to create an instance of

    // NumericFn will cause an error.

//  NumericFn ob = new NumericFn(); // Wrong!

  }

}

The output from the program is shown here.



Reciprocal of 5 is 0.2

Fractional part of 4.234 is 0.234

10 is even.

5 is odd.

Notice that the last line in the program is commented-out. Because 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   285   286   287   288   289   290   291   292   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish