在類中聲明一個(gè)靜態(tài)變量如下 :
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、義烏網(wǎng)站維護(hù)、網(wǎng)站推廣。
static int num=0 ,每次類初始化將num 加一。
靜態(tài)變量為所有類共享,num的大小即為創(chuàng)建的student對(duì)象的數(shù)目
程序稍微改動(dòng)如下:
class student {
String name;
static int num =0;
String id;
float ywscore;
float sxscore;
float sumscore;
float avgscore;
student(String name,String id,float ywscore,float sxscore){
this.name=name;
this.id=id;
this.ywscore=ywscore;
this.sxscore=sxscore;
num ++;
}
float sum(){
sumscore=sxscore+ywscore;
return sumscore;
}
float avg(){
avgscore=(sxscore+ywscore)/2;
return avgscore;
}
int getNum()
{
return num;
}
void showinfo(){
System.out.println("name: "+name);
System.out.println("id: "+id);
System.out.println("mathscore: "+sxscore);
System.out.println("langue: "+ywscore);
System.out.println("avg: "+avg());
System.out.println("sum: "+sum());
}
};
能直接運(yùn)行的:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.io.*;
import javax.naming.BinaryRefAddr;
public class Student implements Serializable{//Serializable是申明這個(gè)類可以被序列化
/**
* @param args
*/
public static void main(String[] args) {
ArrayList Student ss=new ArrayListStudent();
for(int i=0;i5;i++)//初始化5個(gè)學(xué)生
{
Student s=new Student(i,"學(xué)生"+i);
s.chenese_score=i+50;
s.english_score=i+75;
s.math_score=i+80;
ss.add(s);
}
try {
FileOutputStream fos=new FileOutputStream("d:/STUDENT.DAT");
try {
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(int i=0;iss.size();i++)//初始化5個(gè)學(xué)生
{
Student s=ss.get(i);
oos.writeObject(s);//將學(xué)生遍歷出來寫入文件中
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//從文件中讀取一個(gè)學(xué)生出來出來
try {
FileInputStream fis=new FileInputStream("d:/STUDENT.DAT");
try {
ObjectInputStream ois=new ObjectInputStream(fis);
try {
Student s=(Student)ois.readObject();
System.out.println("學(xué)生名為 "+s.name+"的總分為:"+s.getAll_score());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int id;
public String name;
public int chenese_score;
public int math_score;
public int english_score;
public int all_score;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getAll_score()//獲取總成績
{
return this.chenese_score+this.english_score+this.math_score;
}
}
classstudent{Stringname;Stringid;floatywscore;floatsxscore;floatsumscore;floatavgscore;student(Stringname,Stringid,floatywscore,floatsxscore){this.name=name;this.id=id;th...
class student {
String name;
String id;
float ywscore;
float sxscore;
float sumscore;
float avgscore;
student(String name,String id,float ywscore,float sxscore){
this.name=name;
this.id=id;
this.ywscore=ywscore;
this.sxscore=sxscore;
}
float sum(){
sumscore=sxscore+ywscore;
return sumscore;
}
float avg(){
avgscore=(sxscore+ywscore)/2;
return avgscore;
}
void showinfo(){
System.out.println("name: "+name);
System.out.println("id: "+id);
System.out.println("mathscore: "+sxscore);
System.out.println("langue: "+ywscore);
System.out.println("avg: "+avg());
System.out.println("sum: "+sum());
}
};
public class text {
public static void main(String [] args){
student s=new student("McGrady","01",90,99);
s.showinfo();
}
}創(chuàng)建多個(gè)STUDENT對(duì)象 統(tǒng)計(jì)student的個(gè)數(shù)!
class Employee {//雇員類
int age;
//所有信息
}
class Test{
List list = new ArrayList();
//聲明創(chuàng)建五個(gè)對(duì)象
//比如Employee a = new Employee();
//......a.age = 30;
list.add(a);
//五個(gè)對(duì)象都加入List集合中
//然后遍歷取出,比較年齡
for(int i=0;ilist.size;i++)
{
//取出年齡最大的對(duì)象 e
}
//輸出e的所有信息
System.out.println(e.age + "其他信息");
}
第一步:新建數(shù)據(jù)庫
連接的是本地localhost,新建一個(gè)新的數(shù)據(jù)庫名是jdbctest
然后建表t_emp
不會(huì)的話可通過執(zhí)行下方的sql語句建表
CREATE TABLE `t_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
第二步:新建java項(xiàng)目
新建完以后添加mysql驅(qū)動(dòng)的jar包,jar包自己下載
在項(xiàng)目上右鍵鼠標(biāo)屬性,然后
添加jar包,我這里已經(jīng)加載過了
第三步:編寫代碼
package com.gf;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Test {
public static void main(String[] args) throws Exception {
int flag=0;
//1.加載驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//2.獲取連接
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
//3.創(chuàng)建statement
Statement sm=(Statement) conn.createStatement();
//4.執(zhí)行sql語句
flag=sm.executeUpdate("insert into t_emp(name,salary) values('菲菲',34.9)");
if(flag!=0) {
System.out.println("員工信息增加成功");
}else {
System.out.println("添加失敗");
}
}
}
注意點(diǎn):
---------------------------------------------------------------------------------
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
這里需要修改自己本機(jī)的連接信息,不然會(huì)出現(xiàn)連接失敗
最后的執(zhí)行結(jié)果
import java.util.*;
class score
{
public static void main(String[]args)
{
//int [][] score=new int[3][4];
int score;
int sum=0;
int avg;
Scanner sc = new Scanner(System.in);
for(int i=0;i3;i++)//這句話有意思,我們?nèi)绻ザx了一個(gè)數(shù)值,如3那么就不需要寫這個(gè)int [][] score=new int[3][4];那如果吧3給去掉呢,那么就這樣寫for(int i=0;iscore.length;i++)
{
sum=0;
System.out.println("請(qǐng)輸入第"+(i+1)+"個(gè)班級(jí)的成績:");
for(int j=0;j4;j++)//如3那么就不需要寫這個(gè)int [][] score=new int[3][4];那如果吧3給去掉呢,那么就這樣寫for(int i=0;iscore[i].length;i++)
{
System.out.println("第"+(j+1)+"個(gè)學(xué)員的成績:");
score=sc.nextInt();//直到打印為4為止。這就是score=sc.nextInt();的用法。
sum=sum+score;
}
avg=sum/4;
System.out.println("第"+(i+1)+"個(gè)班級(jí)的平均分:"+avg+"分");
}
}
}
for 循環(huán) 當(dāng)內(nèi)層循環(huán)結(jié)束后,我們?cè)谌?zhí)行外層循環(huán)。
打完收工。
看看 這個(gè)思路.