(一)学习总结

1.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?
构造方法是一种特殊的方法,它是一个与类同名且返回值类型为同名类类型的方法。对象的创建就是通过构造方法来完成,其功能主要是完成对象的初始化。当类实例化一个对象时会自动调用构造方法。构造方法用于在创建对象时对其进行初始化。
Person per = new Person() ;
构造方法的重载:
构造方法的名称必须与类的名称一致,重载就是构造方法的参数个数不确定。
如上图所示程序,不能通过编译,因为类中所声明的构造方法参数个数有1个,而主函数中参数个数为0.
public class Test {
public static void main(String args[]) {
Foo obj = new Foo();
}
}
class Foo{
int value;
public Foo(int intValue){
value = intValue;
}
}
运行结果:

不能通过编译
少一个无参构造
public Test(){}
2.运行下列程序,结果是什么?分析原因,应如何修改。
public class Test {
public static void main(String[] args) {
MyClass[] arr=new MyClass[3];
arr[1].value=100;
}
}
class MyClass{
public int value=1;
}
运行结果:

原因:只声明了对象,而没有完成对象实例化
对数组里的每个对象元素,通过new构造方法进行动态或静态实例化。
3.运行下列程序,结果是什么?说明原因。
public class Test {
public static void main(String[] args) {
Foo obj1 = new Foo();
Foo obj2 = new Foo();
System.out.println(obj1 == obj2);
}
}
class Foo{
int value = 100;
}
运行结果:

原因:没有对对象数组中每一个元素分别进行实例化。
4.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明
1)封装性就是对外部不可见,不用对象直接访问类中属性。
2)在一般开发中往往要将类中的属性封装(private)
3)为程序加上封装属性
public class Test {
private String name;
private int age;

public static void main(String[] args) {
	Test per=new Test();
	per.name="张三";
	per.age=-30;
	 per.tell();
}
class Testt{
	String name;
	int age;
	public void tell() {
		System.out.println("姓名: "+name+",年龄: "+age);
	}
}
private void tell() {
	// TODO Auto-generated method stub
	
}	

}
程序编译无法通过,提示错误 属性私有
只要是被封装的属性,必须通过setter和getter 方法设置取得
5.阅读下面程序,分析是否能编译通过?如果不能,说明原因。
(1)

class A{
private int secret = 5;
}
public class Test{
public static void main(String args[]){
A a = new A();
System.out.println(a.secret++);
}
}
运行程序

不能通过编译。secret属于私有属性,不能在类外直接访问。
(2)

public class Test{
int x = 50;
static int y = 200;
public static void method(){
System.out.println(x+y);
}
public static void main(String args[]){
Test.method();
}
}
有错误
修改 static int x = 50;
static int y = 200;
得到 250
6.使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出测试类代码和Book类的部分代码,将代码补充完整。
import javax.swing.Spring;
class Book{
int bookId;
String bookName;
double price;
// 声明静态变量
static int a;
public static a;
//定义静态代码块对静态变量初始化
static {
int a = 0;
}
//构造方法
public Book(String bookName,double price ){
this.bookName=bookName;
this.price =price ;
int a;
a++;
this.bookId=1000+a;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//定义方法求图书总册数
public static int totalBook(){
return a;
}
//重写toString方法
public String toString(){
return "编号"+bookId+"书名"+bookName+"价格"+"price "+"图书总数目为"+a;
}
}
public class Test{
public static void main(String args[]){
Book[] books = {new Book("c语言程序设计",29.3),
new Book("数据库原理",30),
new Book("Java学习笔记",68)};
System.out.println("图书总数为:"+ Book.totalBook());
for(Book book:books){
System.out.println(book.toString());
}
}
}
7.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。
单例模式,是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例
class Sun{
private Sun instance = new Sun();
private Sun(){
}
public static Sun getInstance(){
return instance;
}
}
8.理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由。

public class Test {
String str = new String("你好 ");
char[] ch = { 'w','o','r','l','d' };
public static void main(String args[]) {
Test test = new Test();
test.change(test.str, test.ch);
System.out.print(test.str);
System.out.print(test.ch);
}
public void change(String str, char ch[]) {
str = "hello";
ch[0] = 'W';
}
}

9.字符
字符串拆分 ,将字符串变成字符数组,将部分字符数组变成string
字符串以 开头,以 结尾 ,用startsWith()
10。this
this强调本类中方法,
表示类中属性;
调用本类构造方法;
表示当前对象。
this构造方法目的明确,为类中属性赋值。只要对象一被实例化,就必须打印一行一行“一个新的对象被实例化”信息出来。
this表示当前对象,现在正在 就是当前对象。在Persion类中,不能在main()中。
(二)实验总结
1.用面向对象思想完成评分系统
设计思路:构造person,player,score三个类,每个类set,get,string调用
import java.util.Scanner;

public class Person {

public static void main(String[] args) {
	Player people[];
	judge per=new judge();
	System.out.println("输入选手的人数");
	Scanner input=new Scanner(System.in);
	int xnumn=input.nextInt();//输入选手人数
	System.out.println("输入评委的人数");
	int pnumn=input.nextInt();//输入评委人数
	System.out.println("输入选手编号");
	
	

}

}
class Player{
private String num;
private String name;
private float score;

	public void tell(){
		System.out.println("编号:"+getName()+",姓名"+getName());
	}
	public String getName(){
		return name;
	}
	public void setname(String n){
		name = n;
	}
	public String getnum() {
		return num; // 取得编号
	}

	public void setnum(String m) {

		num = m;
	}

	public float score() {
		return score;
	}

	public void setscore(float s) {
		score = s;
	}
	public void Player(String name, String num) {
		this.name = name;
		this.num = num;

	}
	public String toString(){
		return "选手信息:\n"+"\t选手编号:"+this.getnum()+"\n"+"\t选手姓名:"+this.getName();
	}

}
//getter和setter方法、分数输入方法inputScore(),计算平均分avarage(double score[])
import java.util.Scanner;
import java.util.Arrays;
class judge{
private int pnum;//评委人数
private float score[][];//评委给出的分数
public judge(){

}

public judge(int pnum,int xnum,float score[][]) {
	Scanner input=new Scanner(System.in);
	for(int i=0;i<pnum;i++)
		for(int j=0;j<xnum;j++){
			float score1=(float) input.nextInt();
		}
	}

public static int getMax(int score[][]) {
int max=score[0][0];
for(int i=0;i<score.length;i++) {
	for(int j=0;j<score[i].length;j++) {
		if(score[i][j]>max) {
			max=score[i][j];
		}
	}
}
return max;

}
public static int getMin(int score[][]) {
int min=score[0][0];
for(int i=0;i<score.length;i++) {
for(int j=0;j<score[i].length;j++) {
if(score[i][j]<min) {
min=score[i][j];
}
}
}
return min;
}
public static void ave(int score[][]){
int b,c,sum=0;
double a[];
a=new double[score.length];
b=getMax(score);
c=getMin(score);
double ave;
for(int i=0;i<score.length;i++) {
sum=0;
for(int j=0;j<score[i].length;j++) {
sum+=score[i][j];
}
sum=sum-b-c;
ave=(double)sum/(score[i].length-2);
a[i] =ave;
}
Arrays.sort(a);
for (int i=score.length-1;i>=0;i--) {
System.out.println("第"+(score.length-i)+"位选手的得分为:"+a[i]);
}

}

}
问题:一个一个类创建,比较简单,double,float
解决:一个一个添加,看系统提示
2.Email验证
程序思路:
查找@和。位置,看课本以什么结尾,开头,是boolen型
public class Email {

public static void main(String[] args) {
	// TODO Auto-generated method stub

String str1="2783353477@qq.com";
String str2=new String("2783353477@qq.com");
String str3=str2;
String str4="2783353477@qq.com";

}

}
public static boolean address(String str){
int first,last ;
first=str.indexOf("@");
last=str.indexOf(".");
if((first!=-1&&last!=-1)&&(first<last)&&!(str.startsWith("@"))&&(str.endsWith("com")||str.endsWith("cn")||str.endsWith("net")||str.endsWith("gov")||str.endsWith("edu")||str.endsWith("org"))){
return true;
}
return false;
}
3.查找子串
程序设计思路:将字符串转化成字符数组
import java.util.Scanner;
public class Zifu {
public static void main(String[] args) {
System.out.println("请输入一串字符串");
Scanner input=new Scanner(System.in);
String no=input.next();
System.out.println("请输入指定字符串");
String num=input.next();
System.out.println(find(no,num));
}
public static int find(String no,String num){
int sum=0;
char a[]=no.toCharArray();
char b[]=num.toCharArray();
if(no.contains(num)) {
for(int i=0;i<a.length;i++) {
if(a[i]==b[0]) {
sum++;
}
}
}
return sum;
}
}
4.统计
设计思路
转换大写,然后拆分,定义数组统计
import java.util.Scanner;
public class Co {

public static void main(String[] args) {
System.out.println("hello world");
System.out.println("将"hello world"转化成大写:"+"hello world".toUpperCase());
Scanner input=new Scanner(System.in);
String no=input.next();
num(no);
}
public static void num(String no){
int a=0,b=0,c=0;
String s[] = no.split("\,") ;
for(int x=0;x<s.length;x++){

         if(s[x].endsWith("h")){
             a++;
         }
         if(s[x].endsWith("e")){
             b++;
         }
         
         String n=s[x].substring(0,1);
         System.out.println(n.toUpperCase()+s[x].substring(1,s[x].length()));
        
   }
    System.out.println("h出现的次数为:"+a); 
    System.out.println("e出现的次数为:"+b);     

}
}
问题:只能看到转换成大写,看不到多少个
解决:定义数组
(三)码云

posted on 2018-04-01 18:11  芒果酸奶  阅读(229)  评论(1编辑  收藏  举报