博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CS2312 Lecture 2
阅读量:4886 次
发布时间:2019-06-11

本文共 2691 字,大约阅读时间需要 8 分钟。

Objects and Classes

Objects-Oriented Programming

  • Programming using objects
  • An object represents an entity in the real world
  • Objects have two parts:
    • State:  Properties of an object.
      • Also referred to as field or attributes
    • Behavior: Things the object can do.
      • Also referred to as methods or functions

Why use objects?

  • Modularity: Once we define an object, we can reuse it for other applications.
  • Encapsulation: Programmers don’t need to know exactly how the object works. Just the interface. (Abstraction)
 
The keyword
class tells java that we’re defining a new type of Object.
Classes are a blueprint.
Objects are instances of classes.
Nearly everything in Java (except primitives and arrays) are Objects and have a Class.
The
this keyword means this particular object. Objects know themselves
The
new keyword creates a new object. new must be followed by a constructor

 

Constructor

Constructors provide objects with the data they need to initialize themselves, like “How to Assemble” instructions.
Constructor name is same as the class name
No return type – never returns anything
Usually initialize fields
We can define constructors to take any number of arguments.
All classes need at least one constructor

 

Example:

Defind a class

public class Baby{    String name;    double weighr = 5.0;    boolean isMale;    int numPoops = 0;    Baby[] siblings;    void sayHi(){...}    void eat(double foodWeight){...}}

Using classes

Baby shiloh = new Baby("Shiloh", true);shiloh.sayHi();shiloh.eat(1.0)

 

Static types and methods

static

  • Applies to fields and methods (Share fields and methods to all instances)
  • Means the field/method
    • Is defined for the class declaration,
    • Is not unique for each instance

static field

  • A given class will only have one copy of each of its static fields  
    • This will be shared among all the objects.  
  • Each static field exists even if no objects
    • of the class have been created.    
  • Use the word static to declare a static field.  
  • Only one instance of a static field data for the entire class, not one per instance. 

static/class methods

  • Static methods are shared by all objects of the class
  • One copy for all objects

 But, Have access only to static fields and methods of the class (Cannot access non-static ones)

Example

public class Baby{    static int numBabiesCreated = 0;    String name;    int age;    ...    public int getAge(){        return age;    }    public static Baby older(Baby b1, Baby b2){        if(b1.getAge() > b2.getAge())            return b1;        return b2;    }}

 

We HAVE TO use the static key word on the main method in the class that starts the program

-No objects exist yet for the main method to operate on

 

转载于:https://www.cnblogs.com/charon922/p/8435009.html

你可能感兴趣的文章
TCP和UDP的优缺点及区别
查看>>
MATLAB消除曲线毛刺Outlier Detection and Removal [hampel]
查看>>
【javascript学习——《javascript高级程序设计》笔记】DOM操作
查看>>
高效的SQL语句翻页代码
查看>>
XMLHTTP.readyState的五种状态
查看>>
百度外卖 前端面试题
查看>>
查询树形的根节点
查看>>
HDU 1272 小希的迷宫
查看>>
hdu 5412 CRB and Queries(整体二分)
查看>>
CentOS如何安装linux桌面?
查看>>
Speech and Booth Demo in Maker Faire Shenzhen 2018
查看>>
bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
查看>>
bzoj 2281: [Sdoi2011]黑白棋
查看>>
bzoj 4475: [Jsoi2015]子集选取
查看>>
团队开发7
查看>>
java之静态代理与动态代理
查看>>
软件测试2019:第四次作业
查看>>
201571030335 + 小学四则运算练习软件项目报告
查看>>
LOG收集系统(一):原日志至收集
查看>>
Logstash连接Elasticsearch异常
查看>>