2.单例模式的使用。
3.构造方法的私有化。
4.string的使用,两种构造的不同。
· 数组的长度:数组名称.length 这个没()哈~~
· 字符串的长度:调用的是里面的方法:字符串对象.length() 这个有()哈~
Java:
//静态变量可以在定义时初始化,是所有对象共同产生的
static String city = "chengdu";
String name ;
int age;
//单例模式 一个类只有个一实例化模型
private Info(){}
private static Info instance = new Info();
public static Info getInstance(){
return instance;
}
static void show(){
//静态的方法只能调用静态的变量或静态的方法
// this.name = "zsf";
//this.age = 23;
System.out.println(Info.city);
}
static void show(Info info){
//this.name = "zsf";
//this.age = 23;
System.out.println(info.name);
System.out.println(info.age);
System.out.println(info.city);
}
};
public class StaticTest{
public static void main(String args[]){
//静态方法可以在对象没有实例化之前调用静态方法只能调用静态属性。,
//非静态方法只能对象实例化后才可以调。
/*
Info.show();
Info.show(new Info());
Info info = new Info();
info.name = "zsf";
info.age = 23;
Info.city = "sichuang";
info.show(info);
System.out.println(info.name);
System.out.println(info.age);
System.out.println(info.city);
*/
Info.show();
// 单例模式的调用及使用。
Info info = null;
info = Info.getInstance();
Info info1 = null;
info1 = Info.getInstance();
info.name = "zsf";
info.age = 23;
info1.name ="aa";
info1.age = 23;
System.out.println(info.name);
System.out.println(info.age);
System.out.println(info1.name);
System.out.println(info1.age);
}
}
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
Person(){};
static Person *per;
public:
static Person* getInstance(){
if(per==NULL)
per = new Person();
return per;
}
#if 0
string name;
int age;
static string city;
void setName(string name);
void setAge(int age);
static void show(){
cout<<Person::city<<endl;
}
static void show(Person *p);
#endif
};
Person* Person::per= NULL ;
#if 0
void Person::setName(string name){
this->name = name;
}
void Person::setAge(int age){
this->age = age;
}
void Person::show(Person *p){
cout<<p->name<<endl;
cout<<p->age<<endl;
cout<<p->city<<endl;
}
string Person::city = "sichuang";
#endif
int main(int argc,char *argvp[]){
Person *per2= NULL;
per2 = Person::getInstance();
#if 0
Person *per1 = NULL;
per1 = Person::getInstance();
per2->name = "zsf";
per2->age =23;
#if 0
per1->name ="hello";
per1->age = 34;
cout<<per1->name<<endl;
cout<<per1->age<<endl;
#endif
cout<<per2->name<<endl;
cout<<per2->age<<endl;
/*
Person::show();
Person::show(new Person());
cout<<"==================="<<endl;
Person *per = new Person();
per->name = "hello";
per->age = 23;
per->city = "chengdu";
cout<<per->city<<endl;
per->show(per);
return 0;
*/
#endif
return 0;
}
#include <iostream>
using namespace std;
class Singelton
{
public:
static Singelton* GetInstance()
{
if (instance_ == NULL)
{
instance_=new Singelton();
}
return instance_;
}
private:
Singelton()
{
}
static Singelton* instance_;
};
Singelton *Singelton::instance_;
int main()
{
Singelton *s=Singelton::GetInstance();
Singelton *s2=Singelton::GetInstance();
cout<< s <<endl;
cout<< s2 <<endl;
}
#endif
#if 1
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define MAX_OBJ_NUM 5
class SimpleTest{
private:
SimpleTest(){}
static SimpleTest *simple;
int id;
string info;
static int count;
public:
//同java 静态方法只能用静态变量。
static SimpleTest *getInstance(){
if(count!= MAX_OBJ_NUM){
simple = new SimpleTest();
count++;
return simple;
}else{
cout<<"Enough"<<endl;
return NULL;
}
}
#if 1
void setId(int id){
this->id = id;
}
void setInfo(string info){
this->info = info;
}
int getId(){
return this->id;
}
string getInfo(){
return this->info;
}
#endif
};
//静态变量初始化不同于java。
SimpleTest *SimpleTest::simple = NULL;
int SimpleTest::count = 0;
int main(int argc,char *argv[]){
SimpleTest *simple1 = SimpleTest::getInstance();
cout << simple1 <<endl;
simple1->setId(23);
cout<<simple1->getId()<<endl;
SimpleTest *simple2 = SimpleTest::getInstance();
cout<< simple2 <<endl;
simple2->setId(34);
cout<<simple2->getId()<<endl;
SimpleTest *simple3 = SimpleTest::getInstance();
cout<< simple3 <<endl;
simple3->setId(34);
cout<<simple3->getId()<<endl;
SimpleTest *simple4 = SimpleTest::getInstance();
cout<< simple4 <<endl;
simple4->setId(34);
cout<<simple4->getId()<<endl;
SimpleTest *simple5 = SimpleTest::getInstance();
cout<< simple5 <<endl;
simple5->setId(34);
cout<<simple5->getId()<<endl;
SimpleTest *simple6 = SimpleTest::getInstance();
if(simple6==NULL){
exit(EXIT_FAILURE);
}
cout<< simple6 <<endl;
simple6->setId(34);
cout<<simple6->getId()<<endl;
return 0;
}
#endif