• 201521123076 《JAVA程序设计》第5周学习总结


    1. 本周学习总结

    1.1 尝试使用思维导图总结有关多态与接口的知识点。

    1.2 可选:使用常规方法总结其他上课内容
    • 初步了解了接口的概念,学会使用接口,强化面向对象思想
    • Comparable,Comparator接口的实现及区别。
    • 对abstract抽象的思想进一步理解

    2. 书面作业

    Q1代码阅读:Child压缩包内源代码

    1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误。并分析输出结果。

    A:下面这个函数出现错误不能通过编译

    	public void getParenti(){
            System.out.println(i);
        }  
    

    因为父类的i是private修饰的,对子类不可见,改为protected即可。

    	c.getParenti();
        c.getParentj();
        Other.showParentj(p);  
    

    输出结果是这三个语句。因为Parent类的i和j为protected修饰,故子类可以直接使用,输出i和super.i结果是一样的(j,或者geti都是如此) Other.showParentj(p);因为是static方法,故可以直接用类.方法调用

    1.2 另外一个包中的OutOfParentPackage.java,能否编译通过?提示什么错误?分析原因。

    A:编译无法通过,显示'The type Parent is not visible ',因为Parent无任何修饰,默认包权限,在另一个包就不可见,需用public修饰Parent类才可以。但是这样还不够,之后又会显示j和geti不可见,原因是他们修饰为protected,只允许子类使用,所以这里就不能调用了,将protected改为public即可。

    1.3 回答:如果为了访问到protected修饰的属性或方法应该怎么办?

    A:调用getter,setter函数。


    Q2.abstract进阶:阅读GuessGame抽象类的设计与使用源代码

    2.1 Guess改造前代码很简单,而改造后的代码使用了抽象类、抽象方法看起来很复杂,那这样的改造到底有什么好处呢?

    A: 改造前,没有使用抽象类,只能在控制台输入输出。改造后,使用了抽象类,可根据实际需求在不同界面下输入输出,增强弹性。

    2.2 如果想将该游戏改造成图形界面,应该进行一些什么操作?

    A:设计一个图形界面的GuessGame类,Override输入输出方法。

    2.3 结合该例子,你觉得什么时候应该使用abstract?

    A:当我们需要实现一项功能,而很多不同的方式可以实现这项功能,如USB口传输数据的方式,这时候就可以定义为abstract,当接入不同的设备时可以根据不同的标准来实现这个传输方式。

    2.4 重要:在这个例子中,变化的是什么,不变的是什么?尝试结合abstract、继承等概念进行说明。

    A:不变的是GuessGame这个抽象类。之所以抽象,是因为其只提供了猜数字这个游戏最基本的规则,一些待实现的方法,简单来说就是一个模板,抽象一词区别于‘具体’,是不能被实例化的东西,必须有载体来实现它,
    也就是要通过继承,使之具象化。正如ConsoleGuessGame让GuessGame这个模板变成一个实实在在地能与我们交互的游戏。

    Q3.Comparable与Comparator

    3.1 描述Comparable接口的用途。为什么某个类实现了Comparable接口就可以直接使用Arrays.sort对其进行排序?

    A:Comparable接口让我们可以把相同对象进行比较。我们以前接触的比较无非是简单基本类型的比较,如int。而对象是一个复杂的东西,就如一个人,要比较一个人,必须有个比较的标准,可以是比较身高,体重等等。假如只是说要比较两个人,那怎么能比较呢?Comparable接口正是让我们去规定这个标准,通过什么方式去比较。类实现了Comparable接口就具有可比性,重写comparaTo方法即可规定比较标准。

    3.2 有了Comparable接口为什么还需要Comparator接口呢?

    A:Comparable接口让一个类具有可比性,这个类需要实现Comparable接口,重写comparaTo方法,也就意味的这个类的内部结构发生了改变。而Comoparator接口可以不用改变类的内部结构,只需要定义一个比较器,就可以让对象之间进行比较。Comparable:内部比较,Comparator:外部比较。

    Q4.面向接口案例分析

    阅读Case-StudentDao.zip案例

    4.1 画出类关系图,描述每个类与接口的作用。

    A:
    Student类:一个简单的类
    StudentDao接口:有三个抽象方法:读写,显示Student
    StudenDaoListImpl类:实现StudentDao接口,将读写,显示Studnet操作通过List方式实现
    StudentDaoArrayImpl类:实现StudentDao接口,将读写,显示Studnet操作通过Array方式实现,在类内部定义数组大小。

    4.2 StudenDaoListImpl与StudentDaoArrayImpl有何区别?

    A: List本质也是数组,本例中二者最主要的区别是使用StudentDaoArrayImpl有大小限制,而使用StudenDaoListImpl无大小限制,因为List规定,如果size超出数组大小,那么将会为其增加一个size长度空间。

    Q5.什么是面向接口编程?面向接口编程的好处是什么?不要百度原封不动照搬!

    A:目前我们已经接触到‘面向xx’三个词,一是面向过程,即注重实现某件事的过程。二是面向对象,也就是一提到java大家脱口而出的一个词,所谓面向对象,就是把所需的东西先抽象成一个类,需要用到的时候new一下就出现了一个具体的对象,可以理解为:我们有一个印钞机,我们想要钱的时候,印刷一下就出来了,然后我们就可以使用这个钞票了。夸张一点说,在面向对象的世界里,我们就是创世者,我们定义好每个事物的模板,当我们需要什么的时候,将其实例化即可。接着是面向接口,他是面向对象体系的精髓,面向接口可以说让我们更好的去创建这些模板。飞机和鸟都会飞,但是创造每个对象的时候使用的是不同的模板,而这些模板相同的方法,我们就可以这些相同方法都装起来,提供一个接口,当创建模板的时候实现这个接口,就有做到了不同的模板实现同一个功能,一架直升机is a飞机,一只燕子is a鸟,但是直升机is not a鸟,但是他们both are fly(接口),这就涉及到了重要的面向接口多态思想。
    之前对多态的理解是继承,重写方法,父类调用方法。理解接口,对多态的理解也更深了。就拿USB接口来说,插鼠标和U盘的时候,都能实现数据传输,但是鼠标和U盘不是同一个模板出来的东西。把USB接口当做一个‘接口’,这个接口就实现了不同的设备实现数据传输这个功能。
    好处:1.提高弹性 2.增强可维护性等等

    6.结对编程:面向对象设计(大作业2-非常重要)

    内容:使用Java代码完成上周做的面向对象设计大作业,需要有初步界面。实现的功能尽量简单,少而精,只包含必要的功能,不要追求高大全。
    写出:类图(尽量精简,不用太多子类,两个即可)、系统常用功能描述、关键代码与界面
    形式: 两人依托码云合作完成。请在这里贴出你们的学号、姓名与任务分工。
    注意: 再过几次课要讲Java图形界面编程,到时候要将该系统升级为图形界面。系统的业务逻辑部分应该变化不大,变化大的是输入与输出部分。所以编码的时候,输入(Scanner)与输出(System.out)的代码,请不要将其与某个业务处理函数绑死。
    选做加分: 给出两人在码云上同一项目的提交记录截图,额外加分。注:两个人在码云上新建一个项目。

    6.1
    学生A 学生B 项目地址
    http://www.cnblogs.com/slickghost/ http://www.cnblogs.com/wish-tree/ http://git.oschina.net/SGgroup1/Shopping
    6.2 常用功能描述框架图

    6.3 关键代码
    		/**
    		*main函数入口
    		*/
    	public class Run {
    		
    		public static void main(String[] args) {
    			Function.print("用户登入:");
    			Function.print("账号:");
    			String username = Function.next();
    			Function.print("密码:");
    			String password = Function.next();
    			User user = new User(username,password);
    			Goods[] goods = new Goods[5];
    			ShoppingCart shoppingcart = new ShoppingCart();
    	//		{
    	//			goods[0] = new Book("Java学习笔记", 68.00,100,"林信良");
    	//			goods[1] = new Book("白夜行",30.50,180,"东野圭吾");
    	//			goods[2] = new Phone("红米Note4X",1098.00,68881,"灰色","32G");
    	//			goods[3] = new Phone("华为 nova标配",1899.00,934,"香槟金","32G");
    	//			goods[4] = new Phone("Mate 9",3899.00,16418,"月光银","64G");
    	//			WriteGoods.writeObjectToFile(goods);
    	//		}
    	//		去掉注释把商品导入到本地文件,导入成功后再次注释即可。
    			
    			goods = (Goods[])ReadGoods.readObjectFromFile();//把商品从文件中导出到数组
    	
    				
    		
    			new ConsloeMenu().view(shoppingcart, user, goods);;
    	
    		} 
    	
    	}  
    

    	/**
    	 * 
    	 *抽象菜单类;
    	 */
    	public abstract class Menu {
    		
    		public abstract void view(ShoppingCart shoppingcart , User user , Goods[] goods);
    		//选择查看购物车,当前用户,以及商品
    	}  
    

    	/**
    	 * 在控制台上显示的Menu
    	 * 
    	 *
    	 */
    	public class ConsloeMenu extends Menu {
    		
    		
    		public ConsloeMenu(){
    			
    		}
    		public void view(ShoppingCart shoppingcart , User user , Goods[] goods){
    			int goodbye = 1;
    			while(goodbye == 1){
    			Function.print("请选择操作:");
    			Function.print("1.选购商品");
    			Function.print("2.查看购物车");
    			Function.print("3.查看账户");
    			Function.print("4.退出");
    			int choose = Function.nextInt();
    			for(int i=0;i<=50;i++){
    				System.out.println();
    				} //暴力清屏
    			switch (choose) {
    			case 1:
    			for(int i = 0 ;i <goods.length;i++){
    				Function.print("--------------------------------------");
    				Function.print(i+1+"");
    				goods[i].draw();
    				Function.print("--------------------------------------");
    			}
    			Function.print("请选择你需要购买的商品序号:");
    			int n = Function.nextInt();
    			shoppingcart.add(goods[n-1]);
    			Function.print("选购成功,已加入到购物车!");
    			Function.nextLine();
    			Function.nextLine(); //按任意键继续
    			for(int i=0;i<=50;i++){
    				System.out.println();
    				} //暴力清屏
    				break;
    			case 2:
    				shoppingcart.draw();
    				Function.nextLine();
    				Function.nextLine();
    				for(int i=0;i<=50;i++){
    					System.out.println();
    					} //暴力清屏
    				break;
    				
    			case 3:
    				user.draw();
    				Function.nextLine();
    				Function.nextLine();
    				for(int i=0;i<=50;i++){
    					System.out.println();
    					} //暴力清屏
    				break;
    				
    			default:
    				goodbye = 0;
    				Function.print("GoodBye!");
    				Function.nextLine();
    				Function.nextLine();
    				break;
    			}
    			
    		}//while(true)循环
    			
    		}
    		
    	}  
    

    	/**
    	* 
    	*定义输入输出方式;
    	*/
    	public class Function {
    		private static Scanner scanner = new Scanner(System.in);
    		public static void print(String text) {
    			System.out.println(text);
    			
    		}
    		
    		public static String nextLine() {
    			return scanner.nextLine();
    		}
    		public static int nextInt() {
    			return scanner.nextInt();
    		}
    		public static double nextDouble() {
    			return scanner.nextDouble();
    		}
    		public static  String next() {
    			return scanner.next();
    		}
    	}  
    

    	/**
    	* 
    	*ShoppingCart类;
    	*/
    	public class ShoppingCart {
    	private double totalPrice;
    	private List<Goods> goods = new ArrayList<Goods>();
    	
    	public double getTotalPrice() {
    		return totalPrice;
    	}
    	public void setTotalPrice(double totalPrice) {
    		this.totalPrice = totalPrice;
    	}
    	//以上为get,set方法
    	public void add(Goods goodstemp){
    		this.goods.add(goodstemp);
    	}
    	
    	
    	public void delete(int i ){
    		this.goods.remove(i);
    	}
    	public void clear(){
    		this.goods.clear();
    	}
    	public void pay(){
    		Function.print("you should pay "+totalPrice);
    	}
    	public void draw(){
    		Function.print("你的购物车目前有如下商品:");
    		for(int i = 0 ;i <this.goods.size();i++){
    			Function.print("--------------------------------------");
    			Function.print(i+1+"");
    			goods.get(i).draw();
    			Function.print("--------------------------------------");
    		}
    		
    	}
    	
    	}  
    

    	/**
    	* 
    	*Goods抽象类;
    	*/
    	public abstract class Goods implements Serializable{
    		
    	private String name;
    	private double price;
    	private String ship_address;
    	private int sales_number;
    	public abstract void draw();
    		public Goods(String name,double price,int sales_number){
    			this.name = name;
    			this.price = price;
    			this.sales_number = sales_number;
    		}
    		public String getName() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
    		public double getPrice() {
    			return price;
    		}
    		public void setPrice(double price) {
    			this.price = price;
    		}
    		public String getShip_address() {
    			return ship_address;
    		}
    		public void setShip_address(String ship_address) {
    			this.ship_address = ship_address;
    		}
    		public int getSales_number() {
    			return sales_number;
    		}
    		public void setSales_number(int sales_number) {
    			this.sales_number = sales_number;
    		}
    		
    	}  
    

    	/**
    	* 
    	*User类;
    	*/
    	public class User {
    	private String username;
    	private String password;
    	private String address;
    	private Goods[] GoodsList;//之后补充
    	public User(String username, String password) {
    		this.username = username;
    		this.password = password;
    	}
    	public void draw(){
    		Function.print("welcome to shopping!");
    		Function.print("Hello,"+username);
    	//	Function.print("GoodsList");
    		
    	}
    	}  
    

    	/**
    	* 
    	*Book类;
    	*/
    	public class Book extends Goods implements Serializable{
    	private String author;
    	
    	
    	public Book(String name, double price, int sales_number,String author) {
    		super(name,price,sales_number);
    		this.author = author;
    	}
    	
    	public String getAuthor() {
    		return author;
    	}
    	
    	public void setAuthor(String author) {
    		this.author = author;
    	}
    	
    	public void draw() {
    		Function.print("书名:" + this.getName());
    		Function.print("作者:" + this.author);
    		Function.print("价格:" + this.getPrice());
    		Function.print("销量:" + this.getSales_number());
    	}
    	}  
    

    	/**
    	* 
    	*Phone类;
    	*/
    	public class Phone extends Goods implements Serializable  {
    	
    		private String color;
    		private String memorySize;
    		public Phone(String name, double price, int sales_number,String color,String memorySize) {
    			super(name, price, sales_number);
    			this.color = color;
    			this.memorySize = memorySize;
    		}
    	
    		@Override
    		public void draw() {
    			Function.print("手机型号:" + this.getName());
    			Function.print("颜色:" + this.color);
    			Function.print("内存大小:" + this.memorySize);
    			Function.print("价格:" + this.getPrice());
    			Function.print("销量:" + this.getSales_number());
    		}
    	
    	}  
    

    	/**
    	* 
    	*写入Goods至文件;
    	*/
    	public class WriteGoods {
    		public static void writeObjectToFile(Goods[] obj)
    	    {
    	        File file =new File("test.dat");
    	        FileOutputStream out;
    	        try {
    	            out = new FileOutputStream(file);
    	            ObjectOutputStream objOut=new ObjectOutputStream(out);
    	            objOut.writeObject(obj);
    	            objOut.flush();
    	            objOut.close();
    	            //System.out.println("write object success!");
    	        } catch (IOException e) {
    	            System.out.println("write object failed");
    	            e.printStackTrace();
    	        }
    	    }
    	}  
    

    	/**
    	* 
    	*从文件读取Goods;
    	*/
    	public class ReadGoods {
    		
    		public static Object readObjectFromFile()
    	    {
    	        Object[] temp=null;
    	        File file =new File("test.dat");
    	        FileInputStream in;
    	        try {
    	            in = new FileInputStream(file);
    	            ObjectInputStream objIn=new ObjectInputStream(in);
    	            temp=(Object[]) objIn.readObject();
    	            objIn.close();
    	            //System.out.println("read object success!");
    	        } catch (IOException e) {
    	            System.out.println("read object failed");
    	            e.printStackTrace();
    	        } catch (ClassNotFoundException e) {
    	            e.printStackTrace();
    	        }
    	        return temp;
    	    }
    	}  
    

    Serializable接口实现文件存取对象:
    http://www.cnblogs.com/hrlnw/p/3617478.html

    6.4 运行界面

    3. 码云上代码提交记录及PTA实验总结

    3.1. 码云代码提交记录

    3.2. PTA实验
    • 之前实验已总结;
    • 5-1.实现一下Comparable接口,重写compareTo()方法即可。
    • 5-2.写一个比较器即可。
    • 5-3.要注意使用Integer而非int,否则数组默认元素是0而不是null。
    • 5-4.简单地了解一下内部类。
  • 相关阅读:
    时间复杂度为O(1)的Iveely搜索缓存策略
    数据挖掘十大算法决策树的实现
    编写有效的C# 代码(一)
    数据挖掘十大算法Kmeans之图像区域选择
    asp.net 导出 Excel
    使用XMLSpyDocEditPlugIn2.dll,页面加载失败
    多线程Thread的使用,并使用Thread传参
    ajaxpro.2.dll的使用
    论道WP(二):如何学习WP开发?
    IList<T> 转换成 DataSet
  • 原文地址:https://www.cnblogs.com/slickghost/p/6617411.html
Copyright © 2020-2023  润新知