类总结

        类是一种将抽象转换为用户定义类型的C++工具。它将数据表示和操纵数据的方法组合成一个整体。类的基本思想是数据抽象和封装。
类声明:以数据成员的方式描述数据部分,以成员函数(被称为方法)的方式描述接口函数。类声明用class关键字,一般写在h文件中。
class 类名称(通常类名称的首字母大写)
{
public:
     公有成员(外部接口)
private:
     私有成员 (只允许本类中的函数访问,而类外部的任何函数都不能访问)
protected:
     保护成员(与private类似,差别表现在继承与派生时)
};
        特殊的指针this指针,this指针指向用于调用成员函数的对象,或者说是this是对象的地址。
构造函数的名字与类名相同,不含有返回类型,甚至不能有void修饰。用构造函数创建对象之后,程序跟踪该对象直到其过期为止,当对象过期时,程序会自动调用一个特殊的成员函数完成清理工作。这个函数就是析构函数。
为了保证数据的安全,避免类的成员函数不修改对象的值,应该尽量将成员函数定义为const成员函数。
       static数据成员属于整个类,不属于某个对象。所以只是在类作用域中起作用,不会与全局域中的名字冲突。并且这个类的所有对象共同访问,虽然如此,看起来特别的公有化,但是依然可以实现信息隐藏。
       典型的使用样例是对象计数器,用来统计有多少个对象。使用时可以在构造函数时count++,在析构函数里面count--。
static数据成员的定义:
static  int  obj;
数据成员不属于某个特定的对象,因而不能在构造函数中初始化,必须在类外一定义完类就初始化。
static  const  int  a=2;静态常量数据成员则必须在类中进行初始化。
访问:
(1)成员访问运算符"."、"->"。
(2)类名限定的静态成员名:类名::count;
static成员函数:
static  int  getcount(){ };
静态成员函数只能访问static类型数据成员。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的复数可以用来进行复数运算和处理。以下是Java中复数总结: 1. Java自带的复数:Java中自带了Complex,可以通过导入java.util.Complex包来使用。 2. 复数的定义:复数通常由实部和虚部组成,可以定义一个复数来表示这两个部分。例如: ```java public class Complex { private double real; private double imag; // 构造方法 public Complex(double real, double imag) { this.real = real; this.imag = imag; } // getter和setter方法 public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImag() { return imag; } public void setImag(double imag) { this.imag = imag; } } ``` 3. 复数的运算:复数可以进行加、减、乘、除等运算,例如: ```java public Complex add(Complex other) { double real = this.real + other.real; double imag = this.imag + other.imag; return new Complex(real, imag); } public Complex subtract(Complex other) { double real = this.real - other.real; double imag = this.imag - other.imag; return new Complex(real, imag); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imag * other.imag; double imag = this.real * other.imag + this.imag * other.real; return new Complex(real, imag); } public Complex divide(Complex other) { double real = (this.real * other.real + this.imag * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); double imag = (this.imag * other.real - this.real * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); return new Complex(real, imag); } ``` 4. 复数的常用方法:复数还可以实现一些常用方法,例如获取模长和相角,例如: ```java public double getModulus() { return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2)); } public double getArgument() { return Math.atan2(this.imag, this.real); } ``` 5. 复数的使用:定义好复数后,就可以通过实例化对象来进行复数运算,例如: ```java Complex c1 = new Complex(2, 3); Complex c2 = new Complex(4, -2); Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); Complex quotient = c1.divide(c2); double modulus = c1.getModulus(); double argument = c1.getArgument(); ``` 以上就是Java复数总结
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值