zoukankan      html  css  js  c++  java
  • java菜鸟进阶历程(①)

    0.前言

    本次博客是关于阶段性java作业(第四次,第五次,第六次大作业)的总结

    1.作业过程总结

    ①第四次作业是关于简单的继承,蒙特卡罗方法求圆周率和水文数据处理;第五次作业是关于图形继承与多态和一元多项式求导;第六次作业是关于图形卡片排序,分组问题(涉及类的继承,多态和接口的应用),还有一道加分题--求素数的算法优化设计。关于这三次作业的知识得迭代情况--主要是考察类的继承,在类的继承得基础上加大难度,比如:对正则表达式的运用,对接口的运用,对优化算法设计的应用。

    ②通过作业逐步理解面向对象的封装性、继承性与多态性三大技术特性以及对面向对象三大技术特性之间关系的理解

    封装:在面向对象的使用过程中经常用到,某些成员禁止的对象的外部进行访问,这种情况下需要对成员进行封装.常用的关键字private。成员属性私有化:private $sex=’值’;成员方法的私有化:private function  方法名()

    私有化特征:1.一旦某个成员被私有化(private),那么在对象的外部不能够被修改和访问。2.私有化成员在对象的内部可以使用$this 进行访问。3.声明关键字private 不能够和var 关键字同时使用。在作业中的每个类里都用私有属性,如在class Circle中定义:private double radius

    继承的特性:

    在面向对象的开发过程中,2个类之间可能存在代码大部分重复的情况,为了解决这种问题,引入继承概念,一个类继承另一个类之后就具备该类的所有成员。父类:被其他类继承的类称之为父类。子类:继承其他类的类成为子类,也叫做派生类。继承的实现方式:class 子类名 extends 父类名。如在这几次作业中,图形继承,先有class shape,然后又有cirle类,就可以用继承class circle extends shape,并在circle类里使用super()方法,继承父类的属性。但在类中具有继承关系的类,子类中可以重载父类中的方法。

    多态的特性:对象根据所接收的消息而做出动作。同一消息被不同的对象接受时可产生完全不同的行动,这种现象称为多态性。

    ③在Java作业中难免会遇到各种各样的问题,如:水文数据处理和一元多项式求导中,需要用到正则表达式,然而我对于正则表达式不是很掌握,所以会查阅资料(看Java菜鸟教程中的正则表达式。附上链接:https://www.runoob.com/java/java-regular-expressions.html)还有一些关于继承的使用,会向同学请教,对于第六次作业接口的问题,属实没弄懂,所以在作业中没有使用接口的方法。

    三次作业花费时间比例:每次作业大概都需要三个小时左右

    2.OO设计心得

    ①面向对象设计的基本原则理解(单一职责原则及“开-闭”原则)

    单一职责原则:一个对象应该只包含单一的职责,并且该职责被完整地封装在一个类中。 类的职责要单一,不能将太多的职责放在一个类中,不能使一个类太累。单一职责原则是实现高内聚、低耦合的指导方针。我觉得这是面向对象设计的一种魅力,代码看起来很舒服,改bug也很容易。

      1 import java.util.Scanner;
      2 public class Main {
      3     public static void main (String [] args){    
      4         Scanner input=new Scanner(System.in);
      5         String b[]=new String[1000];//类型
      6         double c[]=new double[1000];//面积
      7         String name[]=new String[1000];
      8         int a[]=new int[100],length=0,flag=1;
      9         double sum1=0,sum2=0,sum3=0,sum4=0,max;
     10        int num = input.nextInt();        
     11         while(num==0)
     12         {
     13             System.out.println("Wrong Format");    
     14                 System.exit(0);
     15         }
     16         while(num != 0)
     17         {   
     18 
     19             if(num <=0 || num > 4)
     20             {     
     21                 System.out.println("Wrong Format");    
     22                 System.exit(0);    
     23                 }
     24             a[length]=num;
     25             num = input.nextInt(); 
     26             length++;//总共输入length个有效;
     27         } 
     28         for(int j=0;j<length;j++) {
     29             if(a[j]==0)
     30             {
     31                 flag=1;
     32                 System.out.println("Wrong Format");  
     33             }
     34             if(a[j]==1) {
     35                 double r=input.nextDouble();
     36                 Circle circle=new Circle(r);  
     37                 if(circle.validate()){
     38                     flag=1;
     39                     b[j]= "Circle";
     40                     c[j]=circle.getArea();
     41                 }
     42             }
     43             if(a[j]==2){
     44                 double l=input.nextDouble();
     45                 double w=input.nextDouble();
     46                 Rectangle rectangle=new Rectangle(l,w);
     47                 if(rectangle.validate()){
     48                     flag=1;
     49                     b[j]= "Rectangle";
     50                     c[j]=rectangle.getArea();
     51                 }
     52             }
     53             if(a[j]==3){
     54                 double s1=input.nextDouble();
     55                 double s2=input.nextDouble();
     56                 double s3=input.nextDouble();
     57                 Triangle triangle = new Triangle(s1,s2,s3);
     58                 if(triangle.validate()){
     59                     flag=1;
     60                    b[j]= "Triangle";
     61                     c[j]=triangle.getArea();
     62                 }
     63             }
     64             if(a[j]==4) {
     65                 double t=input.nextDouble();
     66                 double bot=input.nextDouble();
     67                 double h=input.nextDouble();
     68                 Trapezoid trapezoid = new Trapezoid(t,bot,h);
     69                 if(trapezoid.validate()){
     70                     flag=1;
     71                     b[j]="Trapezoid";
     72                     c[j]=trapezoid.getArea();
     73                 }
     74             }
     75         }
     76         if(flag==0)
     77         System.out.println("Wrong Format");
     78         if(flag==1){
     79             System.out.println("The original list:");
     80             System.out.print("[");
     81             for(int j=0;j<length;j++) {
     82                 System.out.printf(b[j]+":"+String.format("%.2f",c[j])+" ");
     83             }
     84             System.out.print("]");
     85                     
     86             System.out.println("
    The Separated List:");
     87             System.out.print("[");
     88             for(int j=0;j<length;j++) 
     89             {        
     90                 if(a[j]==1)
     91                 System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
     92             }
     93             System.out.print("]");
     94             
     95              System.out.print("[");
     96                 for(int j=0;j<length;j++) 
     97                 {        
     98                     if(a[j]==2)
     99                     System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    100                 }
    101                 System.out.print("]");
    102                 
    103                  System.out.print("[");
    104                     for(int j=0;j<length;j++) 
    105                     {        
    106                         if(a[j]==3)
    107                         System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    108                     }
    109                     System.out.print("]");
    110                     
    111                      System.out.print("[");
    112                         for(int j=0;j<length;j++) 
    113                         {        
    114                             if(a[j]==4)
    115                             System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    116                         }
    117                         System.out.print("]");
    118                         
    119            for(int i=0;i<length;i++)
    120             {            
    121                 for(int j=0;j<length-1-i;j++)
    122                 {       
    123                     
    124                     
    125                     if(c[j]<c[j+1])
    126                     {                    
    127                         double temp=c[j];                    
    128                         c[j]=c[j+1];                    
    129                         c[j+1]=temp;
    130                         String s=b[j];
    131                         b[j]=b[j+1];
    132                         b[j+1]=s;
    133                     }            
    134                 
    135                 }
    136             }
    137             System.out.println("
    The Separated sorted List:");
    138              System.out.print("[");
    139                 for(int j=0;j<length;j++) 
    140                 {        
    141                     if(a[j]==1)
    142                     System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    143                 }
    144                 System.out.print("]");
    145                  System.out.print("[");
    146                     for(int j=0;j<length;j++) 
    147                     {        
    148                         if(a[j]==2)
    149                         System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    150                     }
    151                     System.out.print("]");
    152                     System.out.print("[");
    153                     for(int j=0;j<length;j++) 
    154                     {        
    155                         if(a[j]==3)
    156                         System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    157                     }
    158                     System.out.print("]");
    159                      System.out.print("[");
    160                         for(int j=0;j<length;j++) 
    161                         {        
    162                             if(a[j]==4)
    163                             System.out.print(b[j]+":"+String.format("%.2f",c[j])+" ");                
    164                         }
    165                         System.out.print("]");
    166                         
    167                 
    168                 
    169             
    170             for(int j=0;j<length;j++)
    171             {
    172                 if(a[j]==1)
    173                 {
    174                     sum1+=c[j];
    175                 }
    176                 if(a[j]==2)
    177                 {
    178                     sum2+=c[j];
    179                 }
    180                 if(a[j]==3)
    181                 {
    182                     sum3+=c[j];
    183                 }
    184                 if(a[j]==4)
    185                 {
    186                     sum4+=c[j];
    187                 }
    188             }
    189             max=sum1;
    190             if(sum2>max)
    191                 max=sum2;
    192             if(sum3>max)
    193                 max=sum3;
    194             if(sum4>max)
    195                 max=sum4;
    196             System.out.println("
    The max area:"+String.format("%.2f",max));
    197         }    
    198     }
    199 }
    200 abstract class Shape{
    201     public abstract double getArea();
    202     public abstract boolean validate();
    203     public abstract String toString ();
    204 }
    205 class Circle extends Shape{
    206     private double radius;
    207     public Circle(){
    208 
    209     }
    210     public Circle(double radius){
    211         setRadius(radius);
    212     }
    213     public void setRadius(double radius){
    214         this.radius=radius;
    215     }
    216     public double getRadius(){
    217         return this.radius;
    218     }
    219     @Override
    220     public double getArea(){
    221         return Math.PI*radius*radius;
    222     }
    223     @Override
    224     public boolean validate(){
    225         if(radius>0)
    226         return true;
    227         else
    228         return false;
    229     }
    230     @Override
    231     public String toString(){
    232         return null;
    233     }
    234 }
    235 
    236 
    237 class Rectangle extends Shape{
    238     private double length,width;
    239     public Rectangle(){
    240 
    241     }
    242     public Rectangle(double length,double width){
    243         setLength(length);
    244           setWidth(width);
    245     }
    246     public void setWidth(double width){
    247          this.width=width;       
    248     }
    249     public double getWidth(){
    250          return this.width;
    251     }
    252     public void setLength(double length){
    253         this.length=length;
    254     }
    255     public double getLength(){
    256          return this.length;
    257     }
    258     @Override
    259     public double getArea(){
    260         return length*width;
    261     }
    262     @Override
    263     public boolean validate(){
    264         if(length>0&&width>0)
    265         return true;
    266         else
    267         return false;
    268     }
    269     @Override
    270     public String toString(){
    271         return null;
    272     }
    273 }
    274 class Triangle extends Shape{ 
    275     private double side1,side2,side3;
    276     public Triangle(){ 
    277 
    278     } 
    279     public Triangle(double side1,double side2,double side3){ 
    280         setSide1(side1);
    281         setSide2(side2);
    282         setSide3(side3);
    283     } 
    284     public void setSide1(double side1){
    285         this.side1=side1;
    286     }
    287     public double getSide1(){
    288         return this.side1;
    289     }
    290     public void setSide2(double side2){
    291         this.side2=side2;
    292     }
    293     public double getSide2(){
    294         return this.side2;
    295     }
    296     public void setSide3(double side3){
    297         this.side3=side3;
    298     }
    299     public double getSide3(){
    300         return this.side3;
    301     }
    302     @Override
    303     public double getArea(){  
    304         double l=(side1+side2+side3)/2;
    305         return  Math.sqrt(l*(l-side1)*(l-side2)*(l-side3));      
    306     }
    307     @Override
    308     public boolean validate(){
    309         if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
    310         return true;
    311         else 
    312         return false;
    313     } 
    314     @Override                                                            
    315     public String toString(){
    316         return null;
    317     }
    318 }
    319 class Trapezoid extends Shape{ 
    320     private double topSide,bottomSide,height;
    321     public Trapezoid(){ 
    322 
    323     } 
    324     public Trapezoid(double topSide,double bottomSide,double height){ 
    325         setTopSide(topSide);
    326         setBottomSide(bottomSide);
    327         setHeight(height);
    328     } 
    329     public void setTopSide(double topSide){
    330         this.topSide=topSide;
    331     }
    332     public double getTopSide(){
    333         return this.topSide;
    334     }
    335     public void setBottomSide(double bottomSide){
    336         this.bottomSide=bottomSide;
    337     }
    338     public double getBottomSide(){
    339         return this.bottomSide;
    340     }
    341     public void setHeight(double height){
    342         this.height=height;
    343     }
    344     public double getHeight(){
    345         return this.height;
    346     }
    347     @Override
    348     public double getArea(){  
    349         return  (topSide+bottomSide)*height/2;
    350     }
    351     @Override
    352     public boolean validate(){
    353         if(topSide>0&&bottomSide>0&&height>0)
    354         return true;
    355         else 
    356         return false;
    357     } 
    358     @Override                                                            
    359     public String toString(){
    360         return null;
    361     }
    362 }
    View Code

    注:这就是我不会用接口,然后导致Main类太复杂,改BUG改半天改不出的后果,哭了哭了。。

    开-闭原则:开-闭原则则是对于扩展是开放的,对于修改是关闭的。开闭原则是面向对象的可复用设计的第一块基石,是面向对象设计的目标。

    我看过一篇博客,对这两个原则分析的很到位,在此附上链接(https://blog.csdn.net/iteye_568/article/details/82610391)

    ②OO编程思维的理解(Object oriented)

    面向对象将事物都看成对象,将具有相同特征的实物看作类。通过对类与对象的继承,封装,多态的实现,完成模拟现实从而解决问题。

    ③类设计心得

    设计类要根据题目的要求,再用继承,封装,多态,接口等方法实现类,注意不要再一个类里放太多的方法,不然可能会使程序出现问题。

    3.测试的理解与实践

    ①代码质量保证了,功能才能稳定,稳定可靠的功能就是最好的宣传。保证代码质量就是程序员练内功,内功扎实,才能在上面构建更多的可能性。但往往在平时作业中可能我们的代码都是有漏洞的,但pta开发环境可能检测不到我们的漏洞,所以在平时写代码的时候,需要严谨的思考。对此,建议大家可以看看有关测试方面的书籍--譬如《谷歌测试之道》,《微软测试之道》,《持续交付》,《测试驱动开发》,《自动化测试》。

    ②用Junit也可以对程序进行测试(链接:https://blog.csdn.net/qq_42618969/article/details/81082382)

    4.课程收获

    最大的收获当然是又学到了更多的关于Java的新知识,虽然有些地方还没有完全搞懂,但我会慢慢消化,学好OO设计编程。当然,在收获的同时当然也会又教训,比较大的教训可能就是对Java类理解的不够到位,导致在每次大作业的时候都需要请教同学某个类里的某个方法到底起什么作用。

    5.对课程的建议

    还是老话,希望老师每次能把上课讲得实例代码发出来,对于我这种啥都不会的小白可是有很好的作用的。

  • 相关阅读:
    html中label及加上属性for之后的用法
    Django中利用filter与simple_tag为前端自定义函数的实现方法
    关于自动编译iOS工程,生成app及ipa文件的方法-备
    ios打包ipa的四种实用方法(.app转.ipa)-备
    为GCD队列绑定NSObject类型上下文数据-利用__bridge_retained(transfer)转移内存管理权-备
    GCD使用经验与技巧浅谈--备
    h5与iOS的wkwebview不兼容问题
    cocoaPods 安装和应用
    .a静态库的注意事项
    UIApplication详解再解-备
  • 原文地址:https://www.cnblogs.com/18207130xj/p/12791008.html
Copyright © 2011-2022 走看看