用C#程序实现一个简单的数学运算类 定义一个类AimpleMath; 为类编写普通

用C#程序实现一个简单的数学运算类 定义一个类AimpleMath; 为类编写普通,第1张

见代码

using System

namespace ConsolApplication1

{

    // 简单的数学运算类 SimpleMath

    桥斗public class SimpleMath

    {

        // 加

        public double Add(double a, double b)

        {

            return a + b

        }

        //

        public double Subtract(double a, double b)

        {

            return a - b

        }

        // 乘

        public double Multiply(double a, double b)

        {

            return a * b

        }

        // 除

        public double Divide(double a, double b)

        {

            return a / b

        }

        // 平方根

        public double Sqrt(double a)

        {

            return Math.Sqrt(a)

        }

        // 幂

        public double Power(double a, int n)

        {

            double r = 1

    盯迹        for(int i=1 i<=ni++)

            {    

                r *= a

            }

            return r

        }

    }

    

    class Program

    {

        static void Main(string[] args)

        {

            // 实例化简单算术类SimleMath

            SimpleMath sm = new SimpleMath()

            

            double r = sm.Add(100, 200)

          敏则磨  Console.WriteLine(r)

            

            r = sm.Subtract(200 , 100)

            Console.WriteLine(r)

            

            r = sm.Multiply(2.0, 3.0)

            Console.WriteLine(r)

            

            r = sm.Divide(100, 5)

            Console.WriteLine(r)

            

            r = sm.Sqrt(80)

            Console.WriteLine(r)            

            r = sm.Power(10, 3)

            Console.WriteLine(r)

       }

    }

}

public class Book {

private String strName//表示书名

private String strAuthors//表示作搭升者

private String strPress//表示出版社

private String strISBN//表知喊老示ISBN

private float fPrice//表示价格

public Book()

{

this("书名","作者","出版社","0-0000-00",0)

}

public Book(String name, String authors, String press, String isbn, float price)

{

strName = name

strAuthors = authors

strPress = press

strISBN = isbn

fPrice = price

}

/**

* @return fPrice

*/

public float getFPrice() {

return fPrice

}

/**

* @param price 要设置的 fPrice

*/

public void setFPrice(float price) {

fPrice = price

}

/**

* @return strAuthors

*/

public String getStrAuthors() {

return strAuthors

}

/**

* @param strAuthors 要设置的 strAuthors

*/

public void setStrAuthors(String strAuthors) {

this.strAuthors = strAuthors

}

/**

* @return strISBN

*/

public String getStrISBN() {

return strISBN

}

/**

* @param strISBN 要设置的 strISBN

*/

public void setStrISBN(String strISBN) {

this.strISBN = strISBN

}

/**

* @return strName

*/

public String getStrName() {

return strName

}

/**

* @param strName 要设置的 strName

*/

public void setStrName(String strName) {

this.strName = strName

}

/**

* @return strPress

*/

public String getStrPress() {

return strPress

}

/**

* @param strPress 要设置的 strPress

*/

public void setStrPress(String strPress) {

this.strPress = strPress

}

/* (非 Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

StringBuffer sb = new StringBuffer()

sb.append("渗竖书名:")

sb.append(strName)

sb.append("作者:")

sb.append(strAuthors)

sb.append("出版社:")

sb.append(strPress)

sb.append("ISBN:")

sb.append(strISBN)

sb.append("价格:")

sb.append(fPrice)

return sb.toString()

}

}

package com.commens.test

public class BookTest {

public static void main(String[] args) {

Book book1 = new Book()

System.out.println("图书1的信息:" + book1.toString())

Book book2 = new Book("Java程序设计入门","王伟东","吉林电子出版社","7-900393-11-0/H.415",55.0f)

System.out.println("图书2的信息" + book2.toString())

book1 = book2

System.out.println("图书1的信息:" + book1.toString())

System.out.println("图书1的书名:" + book1.getStrName())

System.out.println("图书1的作者:" + book1.getStrAuthors())

System.out.println("图书1的出版社:" + book1.getStrPress())

System.out.println("图书1的ISBN:" + book1.getStrISBN())

System.out.println("图书1的价格:" + book1.getFPrice())

System.out.println("图书2的书名:" + book2.getStrName())

System.out.println("图书2的作者:" + book2.getStrAuthors())

System.out.println("图书2的出版社:" + book2.getStrPress())

System.out.println("图书2的ISBN:" + book2.getStrISBN())

System.out.println("图书2的价格:" + book2.getFPrice())

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12528969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存