×

java经典代码大全例子

java经典代码大全例子(JAVA程序几个经典的程序)

admin admin 发表于2023-03-27 05:58:45 浏览29 评论0

抢沙发发表评论

本文目录

JAVA程序几个经典的程序


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ResponseToMouse extends Applet implements ActionListener,MouseListener,MouseMotionListener
{
int xPos,yPos,k=0;
Button btnDraw,btnClean,btnErase;
public void init()
{
btnDraw = new Button(“画图“);
btnClean = new Button(“清屏“);
btnErase = new Button(“擦除“);
add(btnDraw);
add(btnClean);
add(btnErase);
btnDraw.addActionListener(this);
btnClean.addActionListener(this);
btnErase.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if(k==1)
{
g.setColor(Color.red);
g.fillOval(xPos-5,yPos-5,10,10);
}
if(k==2)
{
super.update(g);
}
if(k==3)
{
g.setColor(Color.white);
g.fillOval(xPos-5,yPos-5,10,10);
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==btnDraw)
k=1;
if(e.getSource()==btnClean){
k=2;
repaint();
}
if(e.getSource()==btnErase)
k=3;
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
showStatus(“鼠标键按下“);
}
public void mouseReleased(MouseEvent e)
{
showStatus(“鼠标键释放“);
}
public void mouseEntered(MouseEvent e)
{
showStatus(“鼠标进入“);
}
public void mouseExited(MouseEvent e)
{
showStatus(“鼠标移出“);
}
public void mouseDragged(MouseEvent e)
{
xPos=e.getX();
yPos=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
showStatus(“鼠标移动“);
}
public void update(Graphics g){
paint(g);
}
}

java io经典代码


package IO;
import java.io.*;
public class FileDirectoryDemo {
public static void main(String args) {
// 如果没有指定参数,则缺省为当前目录。
if (args.length == 0) {
args = new String { “.“ };
}
try {
// 新建指定目录的File对象。
File currentPath = new File(args);
// 在指定目录新建temp目录的File对象。
File tempPath = new File(currentPath, “temp“);
// 用“tempPath”对象在指定目录下创建temp目录。
tempPath.mkdir();
// 在temp目录下创建两个文件。
File temp1 = new File(tempPath, “temp1.txt“);
temp1.createNewFile();
File temp2 = new File(tempPath, “temp2.txt“);
temp2.createNewFile();
// 递归显示指定目录的内容。
System.out.println(“显示指定目录的内容“);
listSubDir(currentPath);
// 更改文件名“temp1.txt”为“temp.txt”。
File temp1new = new File(tempPath, “temp.txt“);
temp1.renameTo(temp1new);
// 递归显示temp子目录的内容。
System.out.println(“更改文件名后,显示temp子目录的内容“);
listSubDir(tempPath);
// 删除文件“temp2.txt”。
temp2.delete();
// 递归显示temp子目录的内容。
System.out.println(“删除文件后,显示temp子目录的内容“);
listSubDir(tempPath);
} catch (IOException e) {
System.err.println(“IOException“);
}
}
// 递归显示指定目录的内容。
static void listSubDir(File currentPath) {
// 取得指定目录的内容列表。
String fileNames = currentPath.list();
try {
for (int i = 0; i 《 fileNames.length; i++) {
File f = new File(currentPath.getPath(), fileNames[i]);
// 如果是目录,则显示目录名后,递归调用,显示子目录的内容。
if (f.isDirectory()) {
// 以规范的路径格式显示目录。
System.out.println(f.getCanonicalPath());
// 递归调用,显示子目录。
listSubDir(f);
}
// 如果是文件,则显示文件名,不包含路径信息。
else {
System.out.println(f.getName());
}
}
} catch (IOException e) {
System.err.println(“IOException“);
}
}
}
package IO;
import java.io.*;
public class FileExample {
public FileExample() {
super();// 调用父类的构造函数
}
public static void main(String args) {
try {
String outfile = “demoout.xml“;
// 定义了一个变量, 用于标识输出文件
String infile = “demoin.xml“;
// 定义了一个变量, 用于标识输入文件
DataOutputStream dt = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(outfile)));
/**
* 用FileOutputStream定义一个输入流文件,
* 然后用BuferedOutputStream调用FileOutputStream对象生成一个缓冲输出流
* 然后用DataOutputStream调用BuferedOutputStream对象生成数据格式化输出流
*/
BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(
dt, “gbk“));// 对中文的处理
DataInputStream rafFile1 = new DataInputStream(
new BufferedInputStream(new FileInputStream(infile)));
/**
*用FileInputStream定义一个输入流文件,
* 然后用BuferedInputStream调用FileInputStream对象生成一个缓冲输出流
* ,其后用DataInputStream中调用BuferedInputStream对象生成数据格式化输出流
*/
BufferedReader rafFile = new BufferedReader(new InputStreamReader(
rafFile1, “gbk“));// 对中文的处理
String xmlcontent = ““;
char tag = 0;// 文件用字符零结束
while (tag != (char) (-1)) {
xmlcontent = xmlcontent + tag + rafFile.readLine() + ’\n’;
}
NewFile.write(xmlcontent);
NewFile.flush();// 清空缓冲区
NewFile.close();
rafFile.close();
System.gc();// 强制立即回收垃圾,即释放内存。
} catch (NullPointerException exc) {
exc.printStackTrace();
} catch (java.lang.IndexOutOfBoundsException outb) {
System.out.println(outb.getMessage());
outb.printStackTrace();
} catch (FileNotFoundException fex) {
System.out.println(“fex“ + fex.getMessage());
} catch (IOException iex) {
System.out.println(“iex“ + iex.getMessage());
}
}
}
package IO;
import java.io.*;
public class FileRandomRW {
// 需要输入的person数目。
public static int NUMBER = 3;
public static void main(String args) {
Persons people = new Persons[NUMBER];
people = new Persons(“张峰“, 26, 2000, “N“);
people = new Persons(“艳娜“, 25, 50000, “Y“);
people = new Persons(“李朋“, 50, 7000, “F“);
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
“peoplerandom.dat“));
// 将人员数据保存至“peoplerandom.dat”二进制文件中。
writeData(people, out);
// 关闭流。
out.close();
// 从二进制文件“peoplerandom.dat”中逆序读取数据。
RandomAccessFile inOut = new RandomAccessFile(“peoplerandom.dat“,
“rw“);
Persons inPeople = readDataReverse(inOut);
// 输出读入的数据。
System.out.println(“原始数据:“);
for (int i = 0; i 《 inPeople.length; i++) {
System.out.println(inPeople[i]);
}
// 修改文件的第三条记录。
inPeople.setSalary(4500);
// 将修改结果写入文件。
inPeople.writeData(inOut, 3);
// 关闭流。
inOut.close();
// 从文件中读入的第三条记录,并输出,以验证修改结果。
RandomAccessFile in = new RandomAccessFile(“peoplerandom.dat“, “r“);
Persons in3People = new Persons();
// 随机读第三条记录。
in3People.readData(in, 3);
// 关闭流。
in.close();
System.out.println(“修改后的记录“);
System.out.println(in3People);
} catch (IOException exception) {
System.err.println(“IOException“);
}
}
// 将数据写入输出流。
static void writeData(Persons p, DataOutputStream out) throws IOException {
for (int i = 0; i 《 p.length; i++) {
p[i].writeData(out);
}
}
// 将数据从输入流中逆序读出。
static Persons readDataReverse(RandomAccessFile in) throws IOException {
// 获得记录数目。
int record_num = (int) (in.length() / Persons.RECORD_LENGTH);
Persons p = new Persons[record_num];
// 逆序读取。
for (int i = record_num - 1; i 》= 0; i--) {
p[i] = new Persons();
// 文件定位。
in.seek(i * Persons.RECORD_LENGTH);
p[i].readData(in, i + 1);
}
return p;
}
}
class Persons {
private String name;
private int age; // 4个字节
private double salary; // 8个字节
private String married;
public static final int NAME_LENGTH = 20; // 姓名长度
public static final int MARRIED_LENGTH = 2; // 婚否长度
public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8
+ MARRIED_LENGTH * 2;
public Persons() {
}
public Persons(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = “F“;
}
public Persons(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
public String setName(String n) {
name = n;
return name;
}
public int setAge(int a) {
age = a;
return age;
}
public double setSalary(double s) {
salary = s;
return salary;
}
public String setMarried(String m) {
married = m;
return married;
}
// 设置输出格式。
public String toString() {
return getClass().getName() + “[name=“ + name + “,age=“ + age
+ “,salary=“ + salary + “,married=“ + married + “]“;
}
// 写入一条固定长度的记录,即一个人的数据到输出流。
public void writeData(DataOutput out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 写入一条固定长度的记录到随机读取文件中。
private void writeData(RandomAccessFile out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 随机写入一条固定长度的记录到输出流的指定位置。
public void writeData(RandomAccessFile out, int n) throws IOException {
out.seek((n - 1) * RECORD_LENGTH);
writeData(out);
}
// 从输入流随机读入一条记录,即一个人的数据。
private void readData(RandomAccessFile in) throws IOException {
name = FixStringIO.readFixString(NAME_LENGTH, in);
age = in.readInt();
salary = in.readDouble();
married = FixStringIO.readFixString(MARRIED_LENGTH, in);
}
// 从输入流随机读入指定位置的记录。
public void readData(RandomAccessFile in, int n) throws IOException {
in.seek((n - 1) * RECORD_LENGTH);
readData(in);
}
}
// 对固定长度字符串从文件读出、写入文件
class FixStringIO {
// 读取固定长度的Unicode字符串。
public static String readFixString(int size, DataInput in)
throws IOException {
StringBuffer b = new StringBuffer(size);
int i = 0;
boolean more = true;
while (more && i 《 size) {
char ch = in.readChar();
i++;
if (ch == 0) {
more = false;
} else {
b.append(ch);
}
}
// 跳过剩余的字节。
in.skipBytes(2 * (size - i));
return b.toString();
}
// 写入固定长度的Unicode字符串。
public static void writeFixString(String s, int size, DataOutput out)
throws IOException {
int i;
for (i = 0; i 《 size; i++) {
char ch = 0;
if (i 《 s.length()) {
ch = s.charAt(i);
}
out.writeChar(ch);
}
}
}
package IO;
import java.io.*;
import java.util.*;
public class FileRW {
// 需要输入的person数目。
public static int NUMBER = 3;
public static void main(String args) {
Person people = new Person[NUMBER];
// 暂时容纳输入数据的临时字符串数组。
String field = new String;
// 初始化field数组。
for (int i = 0; i 《 4; i++) {
field[i] = ““;
}
// IO操作必须捕获IO异常。
try {
// 用于对field数组进行增加控制。
int fieldcount = 0;
// 先使用System.in构造InputStreamReader,再构造BufferedReader。
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
for (int i = 0; i 《 NUMBER; i++) {
fieldcount = 0;
System.out.println(“The number “ + (i + 1) + “ person“);
System.out
.println(“Enter name,age,salary,married(optional),please separate fields by ’:’“);
// 读取一行。
String personstr = stdin.readLine();
// 设置分隔符。
StringTokenizer st = new StringTokenizer(personstr, “:“);
// 判断是否还有分隔符可用。
while (st.hasMoreTokens()) {
field[fieldcount] = st.nextToken();
fieldcount++;
}
// 如果输入married,则field不为空,调用具有四个参数的Person构造函数。
if (field != ““) {
people[i] = new Person(field,
Integer.parseInt(field), Double
.parseDouble(field), field);
// 置field为空,以备下次输入使用。
field = ““;
}
// 如果未输入married,则field为空,调用具有三个参数的Person构造函数。
else {
people[i] = new Person(field,
Integer.parseInt(field), Double
.parseDouble(field));
}
}
// 将输入的数据保存至“people.dat”文本文件中。
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(“people.dat“)));
writeData(people, out);
// 关闭流。
out.close();
// 从文件“people.dat”读取数据。
BufferedReader in = new BufferedReader(new FileReader(“people.dat“));
Person inPeople = readData(in);
// 关闭流。
in.close();
// 输出从文件中读入的数据。
for (int i = 0; i 《 inPeople.length; i++) {
System.out.println(inPeople[i]);
}
} catch (IOException exception) {
System.err.println(“IOException“);
}
}
// 将所有数据写入输出流。
static void writeData(Person p, PrintWriter out) throws IOException {
// 写入记录条数,即人数。
out.println(p.length);
for (int i = 0; i 《 p.length; i++) {
p[i].writeData(out);
}
}
// 将所有数据从输入流中读出。
static Person readData(BufferedReader in) throws IOException {
// 获取记录条数,即人数。
int n = Integer.parseInt(in.readLine());
Person p = new Person[n];
for (int i = 0; i 《 n; i++) {
p[i] = new Person();
p[i].readData(in);
}
return p;
}
}
class Person {
private String name;
private int age;
private double salary;
private String married;
public Person() {
}
public Person(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = “F“;
}
public Person(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
// 设置输出格式。
public String toString() {
return getClass().getName() + “[name=“ + name + “,age=“ + age
+ “,salary=“ + salary + “,married=“ + married + “]“;
}
// 写入一条记录,即一个人的数据到输出流。
public void writeData(PrintWriter out) throws IOException {
// 格式化输出。
out.println(name + “:“ + age + “:“ + salary + “:“ + married);
}
// 从输入流读入一条记录,即一个人的数据。
public void readData(BufferedReader in) throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, “:“);
name = t.nextToken();
age = Integer.parseInt(t.nextToken());
salary = Double.parseDouble(t.nextToken());
married = t.nextToken();
}
}
package IO;
import java.io.IOException;
public class FileStdRead {
public static void main(String args) throws IOException {
int b = 0;
char c = ’ ’;
System.out.println(“请输入:“);
while (c != ’q’) {
int a = System.in.read();
c = (char) a;
b++;
System.out.println((char) a);
}
System.err.print(“counted\t“ + b + “\ttotalbytes.“);
}
}
//读取输入的数据,直到数据中有Q这个字母然
package IO;
import java.io.*;
public class IOStreamExample {
public static void main(String args) throws IOException {
// 1. 读入一行数据:
BufferedReader in = new BufferedReader(new FileReader(
“FileStdRead.java“));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + “\n“;
}
in.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out.print(“Enter a line:“);
System.out.println(stdin.readLine());
// 2. 从内存中读入
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char) c);
}
// 3. 格式化内存输入
try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(
s2.getBytes()));
while (true) {
System.out.print((char) in3.readByte());
}
} catch (EOFException e) {
System.err.println(“End of stream“);
}
// 4. 文件输入
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter(“IODemo.out“)));
int lineCount = 1;
while ((s = in4.readLine()) != null) {
out1.println(lineCount++ + “: “ + s);
}
out1.close();
} catch (EOFException e) {
System.err.println(“End of stream“);
}
// 5. 接收和保存数据
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(“Data.txt“)));
out2.writeDouble(3.14159);
out2.writeUTF(“That was pi“);
out2.writeDouble(1.41413);
out2.writeUTF(“Square root of 2“);
out2.close();
DataInputStream in5 = new DataInputStream(new BufferedInputStream(
new FileInputStream(“Data.txt“)));
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
} catch (EOFException e) {
throw new RuntimeException(e);
}
// 6. 随机读取文件内容
RandomAccessFile rf = new RandomAccessFile(“rtest.dat“, “rw“);
for (int i = 0; i 《 10; i++) {
rf.writeDouble(i * 1.414);
}
rf.close();
rf = new RandomAccessFile(“rtest.dat“, “rw“);
rf.seek(5 * 8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile(“rtest.dat“, “r“);
for (int i = 0; i 《 10; i++) {
System.out.println(“Value “ + i + “: “ + rf.readDouble());
}
rf.close();
}
}
package IO;
import java.io.*;
/**
* 《p》
* Title: JAVA进阶诀窍
* 《/p》
*
* @author 张峰
* @version 1.0
*/
public class MakeDirectoriesExample {
private static void fileattrib(File f) {
System.out.println(“绝对路径: “ + f.getAbsolutePath() + “\n 可读属性: “
+ f.canRead() + “\n 可定属性: “ + f.canWrite() + “\n 文件名: “
+ f.getName() + “\n 父目录: “ + f.getParent() + “\n 当前路径: “
+ f.getPath() + “\n 文件长度: “ + f.length() + “\n 最后更新日期: “
+ f.lastModified());
if (f.isFile()) {
System.out.println(“输入的是一个文件“);
} else if (f.isDirectory()) {
System.out.println(“输入的是一个目录“);
}
}
public static void main(String args) {
if (args.length 《 1) {
args = new String;
}
args = “d“;
args = “test1.txt“;
args = “test2.txt“;
File old = new File(args), rname = new File(args);
old.renameTo(rname);
fileattrib(old);
fileattrib(rname);
int count = 0;
boolean del = false;
if (args.equals(“d“)) {
count++;
del = true;
}
count--;
while (++count 《 args.length) {
File f = new File(args[count]);
if (f.exists()) {
System.out.println(f + “ 文件己经存在“);
if (del) {
System.out.println(“删除文件“ + f);
f.delete();
}
} else { // 如果文件不存在
if (!del) {
f.mkdirs();
System.out.println(“创建文件: “ + f);
}
}
fileattrib(f);
}
}
}

经典java


Private 语句 在模块级别中使用,用于声明私有变量及分配存储空间。语法Private [WithEvents] varname[([subscripts])] [As [New] type] [,[WithEvents] varname[([subscripts])] [As [New] type]] . . .Private 语句的语法包含下面部分:部分描述WithEvents可选的。关键字,说明 varname 是用来响应由 ActiveX 对象所触发的事件的对象变量。只有在类模块中才是合法的。使用 WithEvents,可以定义任意个所需的单变量,但不能用 WithEvents 创建数组。New 和 WithEvents 不能一起使用。varname必需的。变量的名称;遵循标准的变量命名约定。subscripts可选的。数组变量的维数;最多可以定义 60 维的多维数组。subscripts 参数的使用语法如下: [lower To] upper [,[lower To] upper] . . . 如果不显式指定 lower,则数组的下界由 Option Base 语句控制。如果没有 Option Base 语句则下界为 0。New可选的。使其可以隐式地创建对象的关键字。如果使用 New 声明对象变量,则在第一次引用该变量时创建该对象的新实例,因此不必使用 Set 语句来对该对象引用赋值。New 关键字不能声明任何内部数据类型的变量以及从属对象的实例,也不能与 WithEvents 一起使用。type可选的。变量的数据类型;可以是 Byte、Boolean、Integer、Long、Currency、Single、Double、Decimal(目前尚不支持)、Date、String(对可变长的字符串)、String * length(对定长的字符串)、Object、Variant、用户定义类型或对象类型。所声明的每个变量都要一个单独的 As type 子句。说明Private 变量只能在包含其声明的模块中使用。可以使用 Private 语句声明变量的数据类型。例如,下面的语句声明了一个 Integer 类型的变量。Private NumberOfEmployees As Integer也可以使用 Private 语句来声明变量的对象类型。下面的语句为工作表的新实例声明了一个变量。Private X As New Worksheet如果在定义对象变量时没有使用 New 关键字,则在使用该变量之前,必须使用 Set 语句将一个已有的对象赋给该引用对象的变量。在赋值之前,所声明的这个对象变量有一个特定值 Nothing,这个值表示该变量没有指向任何对象的实例。如果不指定数据类型或对象类型,且在模块中没有使用 Deftype 语句,则按缺省情况该变量为 Variant 类型。可以用带空圆括号的 Private 语句来声明动态数组,然后可以在过程内用 ReDim 语句来定义该数组的维数和元素。如果试图在 Private,Public 或 Dim 语句中重新定义一个已显式定义了大小的数组的维数,就会发生错误。当初始化变量时,数值变量被初始化为 0,变长的字符串被初始化为一个零长度的字符串 (““),而定长的字符串则用 0 填充。Variant 变量被初始化为 Empty。用户自定义类型的变量的每个元素作为各自独立的变量进行初始化。注意 当在过程中使用 Private 语句时,通常将 Private 语句放在过程的开始。
private,空,protected,public 权限
使用private修饰符可以完全隐藏类的成员,使得不能从类外直接访问它们。
不使用修饰符允许同一个包里的任何类直接访问类的成员,但是其他包中的类不可以。
使用protected修饰符允许任何包中的子类或同一包的类的成员。
使用public修饰符允许任何类访问类的成员。
public在程序语言中基本都表示全局变量或者全局函数,他的本意是“公共的”的意思,他表示某个变量或者函数是全局函数,例如:public int a 表示a这个整形变量是全局变量,再例如,声明一个全局函数:public function checked()
........
end function
表示checked这个函数是全局函数
static C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static。前者应用于普通变量和函数,不涉及类;后者主要说明static在类中的作用。
一、面向过程设计中的static
1、静态全局变量
在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变量。我们先举一个静态全局变量的例子,如下:
//Example 1#include 《iostream.h》void fn();static int n; //定义静态全局变量void main(){ n=20; cout《《n《《endl; fn();}void fn(){ n++; cout《《n《《endl;}
静态全局变量有以下特点:
该变量在全局数据区分配内存;
未经初始化的静态全局变量会被程序自动初始化为0(自动变量的值是随机的,除非它被显式初始化);
静态全局变量在声明它的整个文件都是可见的,而在文件之外是不可见的;
静态变量都在全局数据区分配内存,包括后面将要提到的静态局部变量。对于一个完整的程序,在内存中的分布情况如下图:
代码区
全局数据区
堆区
栈区
一般程序的由new产生的动态数据存放在堆区,函数内部的自动变量存放在栈区。自动变量一般会随着函数的退出而释放空间,静态数据(即使是函数内部的静 态局部变量)也存放在全局数据区。全局数据区的数据并不会因为函数的退出而释放空间。细心的读者可能会发现,Example 1中的代码中将
static int n; //定义静态全局变量
改为
int n; //定义全局变量
程序照样正常运行。
的确,定义全局变量就可以实现变量在文件中的共享,但定义静态全局变量还有以下好处:
静态全局变量不能被其它文件所用;
其它文件中可以定义相同名字的变量,不会发生冲突;
您可以将上述示例代码改为如下:
//Example 2//File1#include 《iostream.h》void fn();static int n; //定义静态全局变量void main(){ n=20; cout《《n《《endl; fn();}//File2#include 《iostream.h》extern int n;void fn(){ n++; cout《《n《《endl;}
编译并运行Example 2,您就会发现上述代码可以分别通过编译,但运行时出现错误。 试着将
static int n; //定义静态全局变量
改为
int n; //定义全局变量
再次编译运行程序,细心体会全局变量和静态全局变量的区别。
2、静态局部变量
在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量。
我们先举一个静态局部变量的例子,如下:
//Example 3#include 《iostream.h》void fn();void main(){ fn(); fn(); fn();}void fn(){ static n=10; cout《《n《《endl; n++;}
通常,在函数体内定义了一个变量,每当程序运行到该语句时都会给该局部变量分配栈内存。但随着程序退出函数体,系统就会收回栈内存,局部变量也相应失效。
但有时候我们需要在两次调用之间对变量的值进行保存。通常的想法是定义一个全局变量来实现。但这样一来,变量已经不再属于函数本身了,不再仅受函数的控制,给程序的维护带来不便。
静态局部变量正好可以解决这个问题。静态局部变量保存在全局数据区,而不是保存在栈中,每次的值保持到下一次调用,直到下次赋新值。
静态局部变量有以下特点:
该变量在全局数据区分配内存;
静态局部变量在程序执行到该对象的声明处时被首次初始化,即以后的函数调用不再进行初始化;
静态局部变量一般在声明处初始化,如果没有显式初始化,会被程序自动初始化为0;
它始终驻留在全局数据区,直到程序运行结束。但其作用域为局部作用域,当定义它的函数或语句块结束时,其作用域随之结束;
3、静态函数
在函数的返回类型前加上static关键字,函数即被定义为静态函数。静态函数与普通函数不同,它只能在声明它的文件当中可见,不能被其它文件使用。
静态函数的例子:
//Example 4#include 《iostream.h》static void fn();//声明静态函数void main(){ fn();}void fn()//定义静态函数{ int n=10; cout《《n《《endl;}
定义静态函数的好处:
静态函数不能被其它文件所用;
其它文件中可以定义相同名字的函数,不会发生冲突;
二、面向对象的static关键字(类中的static关键字)
1、静态数据成员
在类内数据成员的声明前加上关键字static,该数据成员就是类内的静态数据成员。先举一个静态数据成员的例子。
//Example 5#include 《iostream.h》class Myclass{public: Myclass(int a,int b,int c); void GetSum();private: int a,b,c; static int Sum;//声明静态数据成员};int Myclass::Sum=0;//定义并初始化静态数据成员Myclass::Myclass(int a,int b,int c){ this-》a=a; this-》b=b; this-》c=c; Sum+=a+b+c;}void Myclass::GetSum(){ cout《《“Sum=“《《Sum《《endl;}void main(){ Myclass M(1,2,3); M.GetSum(); Myclass N(4,5,6); N.GetSum(); M.GetSum();}
可以看出,静态数据成员有以下特点:
对于非静态数据成员,每个类对象都有自己的拷贝。而静态数据成员被当作是类的成员。无论这个类的对象被定义了多少个,静态数据成员在程序中也只有一份拷 贝,由该类型的所有对象共享访问。也就是说,静态数据成员是该类的所有对象所共有的。对该类的多个对象来说,静态数据成员只分配一次内存,供所有对象共 用。所以,静态数据成员的值对每个对象都是一样的,它的值可以更新;
静态数据成员存储在全局数据区。静态数据成员定义时要分配空间,所以不能在类声明中定义。在Example 5中,语句int Myclass::Sum=0;是定义静态数据成员;
静态数据成员和普通数据成员一样遵从public,protected,private访问规则;
因为静态数据成员在全局数据区分配内存,属于本类的所有对象共享,所以,它不属于特定的类对象,在没有产生类对象时其作用域就可见,即在没有产生类的实例时,我们就可以操作它;
静态数据成员初始化与一般数据成员初始化不同。静态数据成员初始化的格式为:
<数据类型><类名>::<静态数据成员名>=<值>
类的静态数据成员有两种访问形式:
<类对象名>.<静态数据成员名> 或 <类类型名>::<静态数据成员名>
如果静态数据成员的访问权限允许的话(即public的成员),可在程序中,按上述格式来引用静态数据成员 ;
静态数据成员主要用在各个对象都有相同的某项属性的时候。比如对于一个存款类,每个实例的利息都是相同的。所以,应该把利息设为存款类的静态数据成员。这 有两个好处,第一,不管定义多少个存款类对象,利息数据成员都共享分配在全局数据区的内存,所以节省存储空间。第二,一旦利息需要改变时,只要改变一次, 则所有存款类对象的利息全改变过来了;
同全局变量相比,使用静态数据成员有两个优势:
静态数据成员没有进入程序的全局名字空间,因此不存在与程序中其它全局名字冲突的可能性;
可以实现信息隐藏。静态数据成员可以是private成员,而全局变量不能;
2、静态成员函数
与静态数据成员一样,我们也可以创建一个静态成员函数,它为类的全部服务而不是为某一个类的具体对象服务。静态成员函数与静态数据成员一样,都是类的内部 实现,属于类定义的一部分。 普通的成员函数一般都隐含了一个this指针,this指针指向类的对象本身,因为普通成员函数总是具体的属于某个类的具体对象的。通常情况下,this 是缺省的。如函数fn()实际上是this-》fn()。但是与普通函数相比,静态成员函数由于不是与任何的对象相联系,因此它不具有this指 针。从这个意义上讲,它无法访问属于类对象的非静态数据成员,也无法访问非静态成员函数,它只能调用其余的静态成员函数。 下面举个静态成员函数的例子。
//Example 6#include 《iostream.h》class Myclass{public: Myclass(int a,int b,int c); static void GetSum();/声明静态成员函数private: int a,b,c; static int Sum;//声明静态数据成员};int Myclass::Sum=0;//定义并初始化静态数据成员Myclass::Myclass(int a,int b,int c){ this-》a=a; this-》b=b; this-》c=c; Sum+=a+b+c; //非静态成员函数可以访问静态数据成员}void Myclass::GetSum() //静态成员函数的实现{// cout《《a《《endl; //错误代码,a是非静态数据成员 cout《《“Sum=“《《Sum《《endl;}void main(){ Myclass M(1,2,3); M.GetSum(); Myclass N(4,5,6); N.GetSum(); Myclass::GetSum();}
关于静态成员函数,可以总结为以下几点:
出现在类体外的函数定义不能指定关键字static;
静态成员之间可以相互访问,包括静态成员函数访问静态数据成员和访问静态成员函数;
非静态成员函数可以任意地访问静态成员函数和静态数据成员;
静态成员函数不能访问非静态成员函数和非静态数据成员;
由于没有this指针的额外开销,因此静态成员函数与类的全局函数相比速度上会有少许的增长;
调用静态成员函数,可以用成员访问操作符(.)和(-》)为一个类的对象或指向类对象的指针调用静态成员函数,也可以直接使用如下格式:
<类名>::<静态成员函数名>(<参数表>)
调用类的静态成员函数。
===============================================================================================
static静态变量声明符。 在声明它的程序块,子程序块或函数内部有效,值保持,在整个程序期间分配存储器空间,编译器默认值0。
是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性。
2、为什么要引入static?
函数内部定义的变量,在程序执行到它的定义处时,编译器为它在栈上分配空间,大家知道,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时,如何实现? 最容易想到的方法是定义一个全局的变量,但定义为一个全局变量有许多缺点,最明显的缺点是破坏了此变量的访问范围(使得在此函数中定义的变量,不仅仅受此函数控制)。
3、什么时候用static?
需要一个数据对象为整个类而非某个对象服务,同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见。
4、static的内部机制:
静态数据成员要在程序一开始运行时就必须存在。因为函数在程序运行中被调用,所以静态数据成员不能在任何函数内分配空间和初始化。
这样,它的空间分配有三个可能的地方,一是作为类的外部接口的头文件,那里有类声明;二是类定义的内部实现,那里有类的成员函数定义;三是应用程序的main()函数前的全局数据声明和定义处。
静态数据成员要实际地分配空间,故不能在类的声明中定义(只能声明数据成员)。类声明只声明一个类的“尺寸和规格”,并不进行实际的内存分配,所以在类声明中写成定义是错误的。它也不能在头文件中类声明的外部定义,因为那会造成在多个使用该类的源文件中,对其重复定义。
static被引入以告知编译器,将变量存储在程序的静态存储区而非栈上空间,静态
数据成员按定义出现的先后顺序依次初始化,注意静态成员嵌套时,要保证所嵌套的成员已经初始化了。消除时的顺序是初始化的反顺序。
5、static的优势:
可以节省内存,因为它是所有对象所公有的,因此,对多个对象来说,静态数据成员只存储一处,供所有对象共用。静态数据成员的值对每个对象都是一样,但它的值是可以更新的。只要对静态数据成员的值更新一次,保证所有对象存取更新后的相同的值,这样可以提高时间效率。
6、引用静态数据成员时,采用如下格式:
《类名》::《静态成员名》
如果静态数据成员的访问权限允许的话(即public的成员),可在程序中,按上述格式
来引用静态数据成员。
7、注意事项:
(1)类的静态成员函数是属于整个类而非类的对象,所以它没有this指针,这就导致
了它仅能访问类的静态数据和静态成员函数。
(2)不能将静态成员函数定义为虚函数。
(3)由于静态成员声明于类中,操作于其外,所以对其取地址操作,就多少有些特殊
,变量地址是指向其数据类型的指针 ,函数地址类型是一个“nonmember函数指针”。
(4)由于静态成员函数没有this指针,所以就差不多等同于nonmember函数,结果就
产生了一个意想不到的好处:成为一个callback函数,使得我们得以将C++和C-based X W
indow系统结合,同时也成功的应用于线程函数身上。
(5)static并没有增加程序的时空开销,相反她还缩短了子类对父类静态成员的访问
时间,节省了子类的内存空间。
(6)静态数据成员在《定义或说明》时前面加关键字static。
(7)静态数据成员是静态存储的,所以必须对它进行初始化。
(8)静态成员初始化与一般数据成员初始化不同:
初始化在类体外进行,而前面不加static,以免与一般静态变量或对象相混淆;
初始化时不加该成员的访问权限控制符private,public等;
初始化时使用作用域运算符来标明它所属类;
所以我们得出静态数据成员初始化的格式:
《数据类型》《类名》::《静态数据成员名》=《值》
(9)为了防止父类的影响,可以在子类定义一个与父类相同的静态变量,以屏蔽父类的影响。这里有一点需要注意:我们说静态成员为父类和子类共享,但我们有重复定义了静态成员,这会不会引起错误呢?不会,我们的编译器采用了一种绝妙的手法:name-mangling 用以生成唯一的标志。
[编辑本段]static 函数
内部函数和外部函数
当一个源程序由多个源文件组成时,C语言根据函数能否被其它源文件中的函数调用,将函数分为内部函数和外部函数。
1 内部函数(又称静态函数)
如果在一个源文件中定义的函数,只能被本文件中的函数调用,而不能被同一程序其它文件中的函数调用,这种函数称为内部函数。
定义一个内部函数,只需在函数类型前再加一个“static”关键字即可,如下所示:
static 函数类型 函数名(函数参数表)
{……}
关键字“static”,译成中文就是“静态的”,所以内部函数又称静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件。
使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名,因为同名也没有关系。
2 外部函数
外部函数的定义:在定义函数时,如果没有加关键字“static”,或冠以关键字“extern”,表示此函数是外部函数:
[extern] 函数类型 函数名(函数参数表)
{……}
调用外部函数时,需要对其进行说明:
[extern] 函数类型 函数名(参数类型表)[,函数名2(参数类型表2)……];
[案例]外部函数应用。
(1)文件mainf.c
main()
{ extern void input(…),process(…),output(…);
input(…); process(…); output(…);
}
(2)文件subf1.c
……
extern void input(……) /*定义外部函数*/
{……}
(3)文件subf2.c
……
extern void process(……) /*定义外部 函数*/
{……}
(4)文件subf3.c
……
extern void output(……) /*定义外部函数*/
{……}
final类 假如说整个类都是final,就表明自己不希望从这个类继续,或者不答应其他任何人采取这种操作。换言之,出于这样或那样的原因,我们的类肯定不需要进行任何改变;或者出于安全方面的理由,我们不希望进行子类化(子类处理)。
除此以外,我们或许还考虑到执行效率的问题,并想确保涉及这个类各对象的所有行动都要尽可能地有效。如下所示:
class SmallBrain {}
final class Dinosaur
{
int i = 7;
int j = 1;
SmallBrain x = new SmallBrain();
void f() {}
}
public class Jurassic
{
public static void main(String args)
{
Dinosaur n = new Dinosaur();
n.f();
n.i = 40;
n.j++;
}
}
注意数据成员既可以是final,也可以不是,取决于我们具体选择。应用于final的规则同样适用于数据成员,无论类是否被定义成final。将类定义成final后,结果只是禁止被继承——没有更多的限制。然而,由于它禁止被继承,所以一个final类中的所有方法都默认为final。因为此时再也无法覆盖它们。所以这与我们将一个方法明确声明为final一样。
可为final类内的一个方法添加final指示符,但这样做没有任何意义。