1 / 2

date example (major changes to the book p.191)

date example (major changes to the book p.191). public class Date{ private int day, month, year; private static final byte D = 1; private static final byte M = 1; private static final short Y = 1900; // constructors public Date(){ this (D, M, Y); }

galen
Download Presentation

date example (major changes to the book p.191)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. date example (major changes to the book p.191) publicclass Date{ privateint day, month, year; privatestaticfinalbyte D = 1; privatestaticfinalbyte M = 1; privatestaticfinalshort Y = 1900; // constructors public Date(){ this(D, M, Y); } public Date(int y){ this(D, M, y); } public Date(int m, int y){ this(D, m, y); } public Date(int d, int m, int y){ setDay(d); setMonth(m); setYear(y); } // selectors (the selectors are also class-methods) publicint getDay(){ return day; } publicint getMonth(){ return month; } publicint getYear(){ return year; } publicvoid setDay(int d){ day=d; } publicvoid setMonth(int m){ month=m; } publicvoid setYear(int y){ year=y; } // methods (for information about the toString method see JDK-Doc „Object“) public String toString(){ return (day+"."+month+"."+year); } }

  2. Wie teste ich meine Klasse? • Zum Testen der Date-Klasse habe ich im Projekt noch die folgende Main-Klasse angelegt.Analog könnt Ihr die Klassen, die Ihr schreibt, testen! publicclass Main { publicstaticvoid main(String[] args) { Date geburtsDatum = new Date(10, 11, 1980); Date defaultDatum = new Date(); System.out.println("geburtsDatum: "+geburtsDatum ); System.out.println("defaultDatum: "+defaultDatum ); // hier wird automatisch die .toString-Methode aufgerufen } } Das zip-File mit dem Beispielcode befindet sich auf der Webseite.

More Related