מבוא ל Groovy אליהו חלסצ'י תכנות מתקדם תרגול מספר 12

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Java Strings in 10 minutes
Principles of Object-Oriented Software Development The language Java.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
© 2008 by «Andres Almiray»; made available under the EPL v1.0 | 03/17/2008 Introduction to Groovy.
Unit Testing Java using Groovy and Mock Objects Walker Hale Human Genome Sequencing Center Baylor College of Medicine.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
CHAPTER 4 SPRING FRAMEWORK Wattanapong suttapak, Software Engineering, school of Information communication Technology, university of phayao.
CSC3170 Introduction to Database Systems
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
A Brief Intro to Scala Tim Underwood. About Me Tim Underwood Co-Founder of Frugal Mechanic Software Developer Perl, PHP, C, C++, C#, Java, Ruby and now.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Groovy in 15 minutes… Johannes Carlén Callista Enterprise AB
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Java Users Group Charleston, SC June 25, 2008 Introduction to Grails by Jason McDonald.
Web Programming Assistant Professor Xiaozhong Liu
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Introduction to 12/13/20111 Prepared by: Vincent Schwarzer, Chih-Hung Hsieh Java Enterprise Application Development.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Grails Mansura Habiba 13 november Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
Classes Dwight Deugo Nesa Matic
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
By Jonathan Smith. Road Map Introduction Company Information Project Overview Java Web Design and Development Summary Relation to IUP Acknowledgments.
Object Oriented Programming Lecture 2: BallWorld.
Современные технологии баз данных Лекция 7. Объектно-реляционное отображение (ORM) Технология отображения объектов какого- либо объектно-ориентированного.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Development of the EDEX plug-in Ingest overview Manual Endpoint LDM DistributionSrv Plugin decoder Plugin Data Object PersistIndexSrv NotificationSrv.
What's Wrong With Java? A Critical Look from Other Languages Aaron Digulla Globus SSC 560.
יסודות מדעי המחשב – תרגול 6
Exercise on Java Basics
Advanced Programming in Java
An Introduction to Java – Part I
Advanced Programming in Java
Coding Standard & Javadoc
Statements, Comments & Simple Arithmetic
Exercise on Java Basics
Introduction to Java Bean
امکانات شیءگرا در جاوا OOP in Java, a Deeper Look
null, true, and false are also reserved.
Introduction to Grails
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Module 2 Variables, Assignment, and Data Types
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Joel Adams and Jeremy Frens Calvin College
Using java libraries CGS3416 spring 2019.
Barb Ericson Georgia Institute of Technology Oct 2005
Encapsulation.
Presentation transcript:

מבוא ל Groovy אליהו חלסצ'י תכנות מתקדם 2 89-211 תרגול מספר 12 תש"ע 2009-2010 מבוא ל Groovy אליהו חלסצ'י

הקדמה Groovy היא שפת סקריפט לפלטפורמת Java שפות סקריפט מאפשרות קלות וגמישות בתכנות הן דינאמיות ולכן יש פחות הגבלות משפה רגילה מאפשרות פיתוח מהיר יותר (פחות קוד לכתוב) על חשבון זמן ריצה - interpreter שפות כגון Python או Ruby עברו בהצלחה לפלטפורמה של Java Jython, JRuby אז למה יש לנו צורך ללמוד עוד שפה??

הקדמה לא צריך ללמוד שפה חדשה כדי לתכנת ב groovy Jython ו JRuby נותנות הרגשה של Python ו Ruby הן גם משתמשות בספריות הקוד שלהן... Groovy נותנת הרגשה של Java ללא הגבלות Groovy מבדילה עצמה מהאחרות באמצעות התחביר שלה והיכולת להשתמש בספריות הסטנדרטיות של Java באופן טבעי

הקדמה אין צורך יותר בקימפול מתאפשר שימוש ב dynamic types תחביר עם פחות אילוצים ניתן להריץ סקריפטים בתוך אפליקציות Java Groovy מגיעה עם interpreter מפרש בזמן ריצה את הקוד ל byte code ומריץ אותו תזכורת: Scala תומכת גם בקימפול הקוד לפני ריצה, ולכן היא סקלבילית גם לתוכנית גדולות...

הבדלים מ Java לא צריך לבצע import מפורש לבאים: == זה equals (לפי תוכן) הגדרת מערך: במקום יש לכתוב לולאות for: הבאים אפשריים: java.io.* java.lang.* java.math.BigDecimal java.math.BigInteger java.net.* java.util.* groovy.lang.* groovy.util.* int[] a = {1,2,3}; int[] a = [1,2,3[ for (int i=0; i < len; i++) {...} for (i in 0..len-1) {...} for (i in 0..<len) {...} len.times {...}

הבדלים מ Java המילה return אופציונאלית this מתוך מתודה סטטית מתכוון למחלקה ברירת המחדל למתודות ולמשתני מחלקה – public ל protected אותה המשמעות כמו ב Java בינתיים לא ניתן ליצור מחלקה בתוך מחלקה אך יש תמיכה ב closures (!) Groovy לא בודק חתימה של throws למתודה משום שאין הבדל מבחינתו בין checked לבין unchecked exceptions

קיים ב Groovy ולא ב Java תמיכה ב Closures תחביר מיוחד לרשימות ומפות תמיכה בביטויים רגולריים כחלק מתחביר השפה תמיכה גם ב static וב dynamic typing לא צריך להצהיר על טיפוס איטרציה פולימורפית Switch Case פולימורפיים ניתן להטמיע ביטויים בתוך מחרוזות לא צריך לשרשר מחרוזות וביטויים בעצמנו

קיים ב Groovy ולא ב Java נוספו המון מתודות עזר ל JDK קל יותר לכתוב Beans Beans הן מחלקות רגילות המשמשות כ struct ב Java bean צריך להגדיר setters ו getters וב Groovy bean לא צריך, נעשה אוטומטית במשפטי if לא צריך לוודא תחילה שהאובייקט אינו null תמיכה ב functional programming לדוג' def fac(n) { n == 0 ? 1 : n * fac(n - 1) }

צעדים ראשונים אתר רשמי: הוראות התקנה: התקנת ה plug-in ל eclipse: http://groovy.codehaus.org/ הוראות התקנה: http://groovy.codehaus.org/Installing+Groovy התקנת ה plug-in ל eclipse: http://dist.springsource.org/release/GRECLIPSE/e3.5/ יצירת פרויקט חדש: Filenewproject… GroovyGroovy project או ע"י

צעדים ראשונים ניצור קובץ חדש ע"י srcnewfile נקרא לו בשם myTest.groovy כבר ניתן לראות ש: ב script אין צורך ב main... Dynamically Typed x יכול להיות כל טיפוס x="Hello World!" println x x = 1 x = new java.util.Date() x = -3.149 x = false

רשימות ומפות פעולות נפוצות map=[:] map.put ("hello", "World!") list=[] list.add ("Hello") list.add ("World!") list+=['today','is',new Date()] println list.join(' ') println list[0] println list[-1] println list.size() list = [4, 5, 6, 6, 4, 7, 2] println list.count(6) println list.sort() map=[:] map.put ("hello", "World!") map+=['today is':new Date(), 89:210, 'advanced':'programming'] println map['hello'] println map.hello map[89]=211 println map[89] println map.size()

closures Closure מזכיר מחלקה אנונימית פנימית אלא ש הוא בעצם מתודה אחת הניתנת לקריאה הוא יכול לקבל פרמטרים (כמה שנרצה) ללא פרמטרים אפשר לעשות שימוש ב it closure = { param -> println("hello ${param}") } closure.call("world!") closure = { greeting, name -> println(greeting + name) } closure.call("hello ", "world!") closure = { println "hello " + it }

closures דוגמאות למעבר על אוספים באמצעות Closures אפשר באמצעות לולאת for אפשר באמצעות מתודות עזר כגון each המקבלות closure כפרמטר it כמו גם הפרמטרים, יכולים להיות מכל טיפוס (איטרציה פולימורפית) כיצד נעבור על map באמצעות closure? for(x in list){ sum+=x } list.each {sum+=it} list.findAll {it>5}.each{sum+=it} list.findAll {x -> if(x>5) sum+=x} list.findAll {if(it>5) sum+=it} println list.every {it>5} println list.any {it>5} map.each{key,val -> println "$key = $val"}

Dynamic Typing קודם ראינו השמה של טיפוסים שונים לאותו ה x ניתן גם להניח מה יש לאובייקט, לדוגמא: class Movie{ int length String name } class Book{ String author def myFavorite(thing){ println "my favorit "+thing.class +" is "+ thing.name movie= new Movie(length:124, name:"Star Wars") book = new Book(name:"The War of the Worlds", author:"H.G. Wells") myFavorite(movie) myFavorite(book)

Dynamic Typing גם במבנה switch case, הפרמטר לא חייב להיות מטיפוס מסוים... def x = 1.23 def result = "" switch ( x ) { case "foo": result = "found foo" // lets fall through case "bar": result += "bar" case [4, 5, 6, 'inList']: result = "list" break case 12..30: result = "range" break case Integer: result = "integer" case Number: result = "number" default: result = "default" } println result == "number"

ביטויים רגולריים זוכרים את Pattern ואת Matcher? בהקשר בוליאני מחזיר אמת אם קיים מופע לתבנית ~== בודק התאמה לתבנית בעצם קורא ל .matches() קיימות מתודות עזר replaceAll replaceFirst ועוד str = "89211" println str ==~ /\d+/ str = "AGCGTTCGAG GCTAGATTAA" if (str =~ 'TT'){ println 'found a match' } ptrn = ~"TT" nStr = (str =~ ptrn).replaceAll('GC') println nStr

beans ב Groovy מספיק לכתוב רק את המאפיינים ה setters וה getters קיימים אוטומטית... כאמור ברירת המחדל היאpublic דוגמא: class Foo { // read only property final String name = "John" // property with public getter and protected setter Integer amount protected void setAmount(Integer amount) { this.amount = amount } // dynamically typed property def cheese }

beans ב Groovy: class Customer { // properties Integer id String name Date date // sample code static void main(args) { def customer = new Customer(id:1, name:"Bean", date:new Date()) println("Hello ${customer.name}") println("your id is "+customer.getId()) }

beans ב Java היינו חייבים להוסיף import Setters ו Getters import java.util.Date; public class Customer { // properties private Integer id; private String name; private Date date; public Integer getId() { return this.id; } public String getName() { return this.name; public Date getDob() { return this.date; public void setId(Integer id) { this.id = id; public void setName(String name) { this.name = name; public void setDob(Date date) { this.date = date; ב Java היינו חייבים להוסיף import Setters ו Getters לאתחל את המאפיינים אחד אחד... // sample code public static void main(String[] args) { Customer customer = new Customer(); customer.setId(1); customer.setName("Bean"); customer.setDob(new Date()); System.out.println("Hello " + customer.getName()); System.out.println("your id is "+customer.getId()); }

סיכום תרגולי הקורס כבר עברה לה שנה... בואו נראה מה למדנו: סמסטר ב': כבר עברה לה שנה... בואו נראה מה למדנו: סמסטר ב': כלים שימושיים Mercurial Maven Tomcat , Jetty תקשורת ו web Sockets Servlets, JSP עבודה מול מסד נתונים ע"י שכבת Hibernate ארכיטקטורות של web services Rest, Comet הרצת תוכנית Java כשירות Service ,Daemon שפות חדשות מבוא ל Scala מבוא ל Groovy סמסטר א': שפת Java היכרות עם Java תחביר Java Java containers Shared libraries כלים שימושיים SVN, ANT, Junit תכנות מקבילי מבוא ל Threads נושאים מתקדמים ב Threads Event driven GUI – SWT Design Patterns Creational, Structural, Behavioral Spring Framework דוגמת Design גדולה

סיכום תרגולי הקורס 100% תכנון מול ביצוע! כבר עברה לה שנה... בואו נראה מה למדנו: סמסטר ב': כלים שימושיים Mercurial Maven Tomcat , Jetty תקשורת ו web Sockets Servlets, JSP עבודה מול מסד נתונים ע"י Hibernate ארכיטקטורות של web services Rest, Comet הרצת תוכנית Java כשירות Service ,Daemon שפות חדשות מבוא ל Scala מבוא ל Groovy סמסטר א': שפת Java היכרות עם Java תחביר Java Java containers Shared libraries כלים שימושיים SVN, ANT, Junit תכנות מקבילי מבוא ל Threads נושאים מתקדמים ב Threads Event driven GUI – SWT Design Patterns Creational, Structural, Behavioral Spring Framework דוגמת Design גדולה 100% תכנון מול ביצוע! אנו בהחלט יכולים להיות גאים בעצמנו על ההספק! אבל האם אנו מתכנתים מתקדמים?

סיכום תרגולי הקורס כדי להיות מתכנתים מתקדמים: תגישו פרויקט איכותי עד ל dead line... תעלו את הסיכומים ל wiki ותלמדו טוב למבחן! תכירו לעומק את השפה בה אתם מתכנתים תהיו הראשונים ללמוד על טכנולוגיות, שפות וכלים חדשים ברגע שהם רואים אור תשכילו להשתמש בכלים, בשפות התכנות ובספריות הקוד שהכי מתאימים לעבודה הנדרשת תחשבו על design (גמיש) לפני שתחשבו על קוד תממשו את בדיקת הקוד לפני כתיבתו... תזכרו שאתם לא מתכנתים לבד בפרויקט... תתרמו לפרויקט קוד פתוח!! הכי חשוב: תצברו ניסיון תארו את הניסיון שלכם בבלוג ;-)

סיכום תרגולי הקורס אני מודה לכם מאד על ההקשבה, הנכונות להשתתף בשיעור ולשאול שאלות, ובעיקר על ההתמדה... נותר לי רק לאחל לכם בהצלחה! בפרויקט, במבחן, בהמשך התואר ובכלל... אלי.