java oop

一:oop  面向对象:

刚开始学编程语言的时候,习惯过程编程,就象写shell的时候也是用命令进行堆叠。但是这种如果修改起来比较麻烦,过一阵子,开始用函数式编程。

但是也不方便,尤其是在功能复杂的时候,写的函数比较多,改起来也比较麻烦,也不好定位。到最终接触到oop即面向对象编程。起初接触oop的时候,感觉一脸懵逼状态。感觉自己的不会写代码。

归其原因是:之前是函数调用,而oop是通过对象进行调用。在调用之前先初始化类的对象(在python里叫做构造方法,在java里是构造器),然后通过对象来调用类中的方法。

java是面向对象的编程语言,一切同类来实现。而python不是。

所以在oop是java的基础。一切借对象,如同linux 一切皆文件!!

由类构造的对象的过程叫做创建类的对象的过程。

java中所有的代码的编写都在某个类中。在oop中编程中,没有顶部的概念,在过程化编程中,从main方法开始进行编程,一次编写自己的方法,在类中没有这个所谓的顶部,用户根据项目编写不同的类型,在main方法中进行实例化对象进行方法的调用。

 1 class Student{
 2     String Deatil(String x,String y){
 3         String sex=x;
 4         String name=y;
 5         return sex+' '+name;
 6     }
 7 }
 8 
 9 public class Main {
10 
11     public static void main(String[] args) {
12     // write your code here
13         Student stu=new  Student();
14         String ret=stu.Deatil("男","evil");
15         System.out.println(ret);
16 
17     }
18 }

类的特性:封装、继承、非多态。

封装:也叫做数据隐藏。

从形式上来看:封装不过是将数据和行为组合在一个包中,并对对象的使用者隐藏了数据的实现方式。

实例域:对象中的数据称为实例域。

方法:操纵数据的过程叫做方法。

对于特定的类实例(对象),都有特定的实例域值。这些值的集合就是对象的状态。

封装的原则:绝对不能让类中的方法直接访问其他类的实例域!程序仅通过对象方法与对象的数据进行交互。

继承:面向对象的另一个特性,可以通过扩展一个类来建立另一个新的类。在java中的类都是源自object。

新扩展的类有继承的类所有方法和属性,新类还可以定义自己的方法和属性。

二 对象

对象的三大特性:

状态--->每个对象都保存着描述当前特征信息。对象的状态的改变必定是通过方法调用的行为来实现。

标识--->每一个对象都有一个唯一的身份。比如:我在京东上下的2次订单,他们的订单号就是不一样。

行为-->具有哪些方法调用。

这三个特性都彼此相互影响,当对象的状态发生变化,他可调用的方法也不一样,比如订单:处于配送状态!

就不可以使用退货或者修改订单地址。反之,如果你的订单对象空的(null)的话,那就没有配送等状态!

三:类之间的关系

1:继承:“is-a”新类B继承A类的方法,又扩展新的方法。

2:依赖:"uses-a" 类之间的 存在调用关系。比如A类的对象调用B类的对象。

3:聚合:"has-a" A类包含B类。

四:预定义类

java 是oop编程,但是并不是 所有的类都有面向对象的特征,比如说Math 类,我们不比初始化对象,可以直接进行调用。

 1 class Student{
 2     double Deatil(){
 3         double re=Math.random();
 4 
 5         return re;
 6     }
 7 }
 8 
 9 public class Main {
10 
11     public static void main(String[] args) {
12         // write your code here
13         Student stu=new  Student();
14         double ret=stu.Deatil();
15         System.out.println(ret);
16 
17     }
18 }

 五:方法的调用

调用类的方法,是通过类的对象来进行调用。通过对象调用类的方法。(静态方法可以用类进行调用)。

初始化类的对象,需要类的构造器初始化,构造器和类的名字一样构造对象需要加关键new。(外部引用数据需要关键new,内部数据(四项八类不需要关键字new))

 1 import java.util.Date;
 2 
 3 class Student{
 4     Date Deatil(){
 5         Date re=new Date();
 6 
 7         return re;
 8     }
 9 }
10 
11 public class Main {
12 
13     public static void main(String[] args) {
14         // write your code here
15         Student stu=new  Student();
16         Date ret=stu.Deatil();
17         System.out.println(ret);
18 
19     }
20 }

注意:

      1:对象和对象变量:如下

 1 import java.util.Date;
 2 
 3 class Student{
 4     String Deatil(){
 5         Date re;
 6         re=re.toString();
 7         Date ret=new Date();
 8         return re;
 9     }
10 }

如上;我们定义一个变量Date类型变量re,但是并没有初始化。所以在引用Date的方法的时候会报错 。

对象变量和对象是不一样,只有对象变量进行初始化之后,对象变量才能引用对象的方法。否则报对象未进行初始化。

  2:对象的变量只是引用对象而已,而不是包含对象!在java中任何对象变量的值,都是对存储在另一个地方的对象引用,new操作符,只是返回该对象的一个引用值。

1  Date ret=new Date();

我们可以用null表示对象变量没有引用对象。

 六:访问器和更改器

访问器:像一些类中的get方法,只是访问对象的状态信息,并不做对象的状态的更改叫做访问器。

更改器:像一些类中的add、del等方法,更改对象的状态信息的方法叫做更改器。

通常在访问器的方法在前缀加get字样,更改器也是一样。。。

 1 import java.util.GregorianCalendar;
 2 import java.util.Date;
 3 
 4 class Student{
 5     Date Deatil(){
 6         GregorianCalendar re=new  GregorianCalendar();
 7         return re.getTime();
 8 
 9 
10     }
11 }
12 
13 public class Main {
14 
15     public static void main(String[] args) {
16         // write your code here
17         Student stu=new  Student();
18         Date ret=stu.Deatil();
19         System.out.println(ret);
20 
21     }
22 }

Date和CregorianCalendar转换

 1 import java.util.GregorianCalendar;
 2 import java.util.Date;
 3 
 4 class Student{
 5     Date Deatil(){
 6         GregorianCalendar re=new  GregorianCalendar();
 7         return re.getTime();
 8 
 9 
10     }
11 }
12 
13 public class Main {
14 
15     public static void main(String[] args) {
16         // write your code here
17         Student stu=new  Student();
18         Date ret=stu.Deatil();
19         Get_Year get_y=new Get_Year();
20         int year=get_y.get_Year(ret);
21         System.out.println(ret);
22         System.out.println(year);
23 
24     }
25 }
26 class  Get_Year{
27     int get_Year(Date X){
28         GregorianCalendar cal=new GregorianCalendar();
29         cal.setTime(X);
30         return cal.get(cal.YEAR);
31     }
32 }

注意的是:日历的获取月份的时候,比当前的月份少1.

 1 import java.util.GregorianCalendar;
 2 import java.util.Date;
 3 
 4 class Student{
 5     Date Deatil(){
 6         GregorianCalendar re=new  GregorianCalendar();
 7         return re.getTime();
 8 
 9 
10     }
11 }
12 
13 public class Main {
14 
15     public static void main(String[] args) {
16         // write your code here
17         Student stu=new  Student();
18         Date ret=stu.Deatil();
19         Get_Year get_y=new Get_Year();
20         int year=get_y.get_Year(ret);
21         Get_M get_m=new Get_M();
22         int month=get_m.get_M(ret);
23         System.out.println(ret);
24         System.out.println(year);
25         System.out.println(month);
26 
27     }
28 }
29 class  Get_Year{
30     int get_Year(Date X){
31         GregorianCalendar cal=new GregorianCalendar();
32         cal.setTime(X);
33         return cal.get(cal.YEAR);
34     }
35 }
36 class  Get_M{
37     int get_M(Date X){
38         GregorianCalendar cal=new GregorianCalendar();
39         cal.setTime(X);
40         return cal.get(cal.MONTH);
41     }
42 }

 获取当前时间戳:

 1 import java.util.Date;
 2 import java.text.SimpleDateFormat;
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // write your code here
 7         Student stu=new  Student();
 8         Date ret=stu.Deatil();
 9         SimpleDateFormat fomate=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
10         String str=fomate.format(ret);
11         System.out.println(str);
12 
13     }
14 }
15 
16 2017-10-04 00:50:31
原文地址:https://www.cnblogs.com/evilliu/p/7615624.html