class CBook{
public:
static double price;//需要通过类外来进行初始化
}
int main(void){
CBook book;
book.price;//通过对象来访问
CBook::price//通过类名来访问
return 0;
}
class CBook{
public:
static double price;
CBook book;//非法定义,不允许在该类中定义所属类的对象
static CBook m_book;//正确
CBook *book;//正确
};
class CBook{
public:
int pages;
static double price;
//定义一个函数,以静态数据作为默认参数
void outPutInfo(int data = price){
//实现
}
//错误定义,普通数据不能作为默认参数
void outPutPage(int data = pages){
//实现
}
};
static void outPut();
static void outPut() const;//错误定义
//错误的定义
static void CBook::outPutInfo(){
//实现
}
class CBook{
public:
int pages;
void outputPages(){
cout<<pages<<endl;
}
};
int main(){
CBook book1,book2;
book1.pages = 10;
book2.pages = 20;
book1.outputPages();
book2.outputPages();
return 0;
}
//函数调用
book1.outputPages(&book1);
//函数实现
void outputPages(CBook *this){//隐含的this指针
cout<<this->pages<<endl;//使用this指针访问数据成员
}
/**
* 本示例为了说明this的三种用法!
*/
package test;
public class ThisTest {
private int i=0;
ThisTest(int i){
//(1)this.i表示成员变量i,i表示参数
this.i=i+1;
}
ThisTest(String s){
System.out.println("String constructor: "+s);
}
ThisTest(int i,String s){
//(2)this调用第二个构造器
this(s);
this.i=i++;
}
public ThisTest increment(){
this.i++;
//(3)返回的是当前的对象
return this;
}
public static void main(String[] args){
ThisTest tt0=new ThisTest(10);
ThisTest tt1=new ThisTest("ok");
ThisTest tt2=new ThisTest(20,"ok again!");
System.out.println(tt0.increment().increment().increment().i);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有