Modern Programming Languages

Programming languages

  1)Structured programming SQL

  2)Procedural programming c 

  3)OO programming c++ java

Procedural Programming vs OO programing

  我自己的感觉,面向过程编程实现小功能是可以的,但是要实现一个很大的工程是比较难的。因为面向过程,一般都是你需要经过一种方法(运算)去得到一个结果,这样主要的问题就是你的函数能不能够完成所需的要求。但是OO编程给我感觉是它更会“利用”,多态性就是围绕着怎么利用进行研究的,比如说,怎么继承等。就像是一个类回去寻找与之相关的类,所得代码量减少。OOP总体上也会比面向过程编程要复杂许多。

Evolution of programming languages(反正就是越来越人性化,越来越利于书写)

···Machine Languages

In the earliest days of computers, the only programming languages available were machine languages. Each computer had its own machine language, which was made of streams of 0s and 1s. The only language understood by a computer is machine language.

```Assembly languages

The next evolution in programming came with the idea of replacing binary code for instruction and addresses with symbols or mnemonics Much more “human friendly” than the machine language

```High-level languages

Although assembly languages greatly improved programming efficiency, they still required programmers to concentrate on the hardware they were using Working with symbolic languages was also very tedious, because each machine instruction had to be individually coded. The desire to improve programmer efficiency and to change the focus from the computer to the problem being solved led to the development of high-level languages E.g., Pascal, C, C++, Java, C#, BASIC…

                                           

 

conditional statements

  if else

Iteration

  'Iteration' in this case means doing something more than once.

Pass by value

指的是在方法调用时,传递的参数是按值的拷贝传递
public class TempTest {
private void test1(int a){
//做点事情
}
public static void main(String[] args) {
TempTest t = new TempTest();
int a = 3;
t.test1(a);//这里传递的参数a就是按值传递
}
}
按值传递重要特点:传递的是值的拷贝,也就是说传递后就互不相关了。
示例如下:
public class TempTest {
private void test1(int a){
a = 5;
System.out.println("test1方法中的a="+a);
}
public static void main(String[] args) {
TempTest t = new TempTest();
int a = 3;
t.test1(a);//传递后,test1方法对变量值的改变不影响这里的a
System.out.println(”main方法中的a=”+a);
}
}
运行结果是:
test1方法中的a=5
main方法中的a=3

Pass by reference

指的是在方法调用时,传递的参数是按引用进行传递,其实传递的引用的地址,也就是变量所对应的内存空间的地址。
public class TempTest {
private void test1(A a){
}
public static void main(String[] args) {
TempTest t = new TempTest();
A a = new A();
t.test1(a); //这里传递的参数a就是按引用传递
}
}
class A{
public int age = 0;
}
引用传递
的重要特点:传递的是值的引用,也就是说传递前和传递后都指向同一个引用(也就是同一个内存空间)。
public class TempTest {
private void test1(A a){
a.age = 20;
System.out.println("test1方法中的age="+a.age);
}
public static void main(String[] args) {
TempTest t = new TempTest();
A a = new A();
a.age = 10;
t.test1(a);
System.out.println(”main方法中的age=”+a.age);
}
}

class A{
public int age = 0;
}
运行结果如下:
test1方法中的age=20
main方法中的age=20

三、说明
1:“在Java里面参数传递都是按值传递”这句话的意思是:按值传递是传递的值的拷贝,按引用传递其实传递的是引用的地址值,所以统称按值传递。
2:在Java里面只有基本类型和按照下面这种定义方式的String是按值传递,其它的都是按引用传递。就是直接使用双引号定义字符串方式:String str = “ccy”;

向上转型:

  package a.b;

  public class A {

  public void a1() {

         System.out.println("Superclass");

  }

  }

  A的子类B:

  package a.b;

  public class B extends A {

  public void a1() {

         System.out.println("Childrenclass"); //覆盖父类方法

  }

         public void b1(){} //B类定义了自己的新方法

  }

  C类:

  package a.b;

  public class C {

  public static void main(String[] args) {

         A a = new B(); //向上转型

         a.a1();

  }

  }

向下转型:

子类转型成父类是向上转型,反过来说,父类转型成子类就是向下转型。但是,向下转型可能会带来一些问题:我们可以说麻雀是鸟,但不能说鸟就是麻雀。来看下面的例子:

  A类:

  package a.b;

  public class A {

  void aMthod() {

         System.out.println("A method");

  }

  }

  A的子类B:

  package a.b;

  public class B extends A {

  void bMethod1() {

         System.out.println("B method 1");

  }

  void bMethod2() {

         System.out.println("B method 2");

  }

  }

  C类:

  package a.b;

  public class C {

       public static void main(String[] args) {

              A a1 = new B(); // 向上转型

              a1.aMthod();    // 调用父类aMthod(),a1遗失B类方法bMethod1()、bMethod2()

              B b1 = (B) a1; // 向下转型,编译无错误,运行时无错误

              b1.aMthod();    // 调用父类A方法

              b1.bMethod1(); // 调用B类方法

              b1.bMethod2(); // 调用B类方法

              A a2 = new A();

              B b2 = (B) a2; // 向下转型,编译无错误,运行时将出错

              b2.aMthod();

              b2.bMethod1();

              b2.bMethod2();

       }

  }

  从上面的代码我们可以得出这样一个结论:向下转型需要使用强制转换。运行C程序,控制台将输出:      

          Exception in thread "main" java.lang.ClassCastException: a.b.A cannot be cast to a.b.B at a.b.C.main(C.java:14)

  A method

  A method

  B method 1

  B method 2

  其实黑体部分的向下转型代码后的注释已经提示你将发生运行时错误。为什么前一句向下转型代码可以,而后一句代码却出错?这是因为a1指向一个子类B的对象,所以子类B的实例对象b1当然也可以指向a1。而a2是一个父类对象,子类对象b2不能指向父类对象a2。那么如何避免在执行向下转型时发生运行时ClassCastException异常?使用5.7.7节学过的instanceof就可以了。我们修改一下C类的代码:

  A a2 = new A();

  if (a2 instanceof B) {

    B b2 = (B) a2;

    b2.aMthod();

    b2.bMethod1();

    b2.bMethod2();

  }

  这样处理后,就不用担心类型转换时发生ClassCastException异常了。

Static Fields and Methods:

  Static fields: A static field belongs to the class Do not change from one instance to the other

    Static Methods:

   Do not need to create an object of the class in order to call the method.

  Can be called through the class name Classname.staticmethod(); or staticmethod()

  if in the same class Can not be overridden Can only call other static methods Only access static fields



posted @ 2017-12-14 21:40  CaiCongyu  阅读(315)  评论(0编辑  收藏  举报